BPLCK205D Module 4 Programs
BPLCK205D Module 4 Programs
Opening file for performing read, write operations using >> and <<
operators respectively.
object EntryFile for class ofstream and read the data from keyboard using
cin>> till the word End entered by user and the same time the entered
data from the keyboard is written to the file “abc.txt” using EntryFile<<.
DisplayFile for the class ifstream then read the data from the file using
Note: the operator >> will not take white space as input character so the
space.
#include <iostream>
#include <string>
#include <fstream>
int main()
while(true)
break;
//input/read mode
while(DisplayFile.eof()==0)
cout << OutputLine << "\n"; // displaying the data on the screen
DisplayFile.close();
return 0;
Opening file for performing read, write operations using get() and
object WriteFile for class ofstream and read the data from keyboard using
cin.get() function till the character $ is entered by user and the same
time the entered data from the keyboard is written to the file “xyz.dat”
ReadFile for the class ifstream then read the data from the file using
include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
int main()
char ch;
ofstream WriteFile("xyz.dat");
while(true)
cin.get(ch);
if(ch == '$')
break;
WriteFile.put(ch);
WriteFile.close();
cout<<"\n";
ifstream ReadFile("xyz.dat");
while(ReadFile.eof()==0)
ReadFile.get(ch);
cout.put(ch);
ReadFile.close();
cout<<"\n";
return 0;
character array.
character array.
Note: When we read a string in a file, the file does not take next line
character as input. Thus, pressing the <Enter> key terminates the string
entry, but the file will not contain that <Enter>. When the file is read back,
#include <iostream>
#include <cstring>
#include <fstream>
int main()
ofstream EntryFile("FewLines.dat");
while(true)
cin.getline(InputLine, 80);
if(!strcmp(InputLine, "End"))
break;
EntryFile.close();
ifstream DisplayFile("FewLines.dat");
while(!DisplayFile.eof())
DisplayFile.getline(OutputLine, 80);
DisplayFile.close();
return 0;
#include <iostream>
#include <fstream>
struct student
int RollNo;
char Name[30];
char Address[40];
};
int main()
ofstream PLC_Out;
if(!PLC_Out.is_open())
do
ReadStudent(E_Section);
if(PLC_Out.fail())
} while(Continue != 'n');
PLC_Out.close();
return 0;
C++ program to open binary file in input/read mode and read data
#include <iostream>
#include <fstream>
#include <string>
struct student
int RollNo;
char Name[30];
char Address[40];
};
int main()
while(!PLC_In.eof())
if(PLC_In.fail())
break;
WriteStudent(E_Section);
PLC_In.close();
return 0;