File Handling in C++
File Handling in C++
As we know, at the time of execution, every program comes in main memory to execute. Main
memory is volatile and the data would be lost once the program is terminated. If we need the
same data again, we have to store the data in a file on the disk. A file is sequential stream of
bytes ending with an end-of-file marker.
Text Files
Binary Files
ofstream class : Provides methods for writing data into file. Such as, open(), put(),
write(), seekp(), tellp(), close(), etc.
ifstream class : Provides methods for reading data from file. Such as, open(),get(),
read(), seekg(), tellg(), close(), etc.
fstream class : Provides methods for both writing and reading data from file. The
fstream class includes all the methods of ifstream and ofstream class.
ofstream fout;
fout.open("filename"); // Open file for writing
ifstream fin;
fin.open("filename"); // Open file for reading
fstream f;
f.open("filename",mode);
Mode Purpose
ios::out Open a text file for writing. If it doesn't exist, it will be created.
Open a text file for appending. Data will be added at the end of the existing file. If
ios::app
file doesnt exist, it will be created.
ios::nocreate The file must already exist. If file doesn't exist, it will not create new file.
ios::trunc If the file already exists, all the data will lost.
ofstream obj;
obj.put(char ch);
Example of put() function
#include<fstream.h>
#include<conio.h>
void main()
{
ofstream fout;
char ch;
fout.open("demo.txt"); //Statement 1
do
//Statement 2
{
cin.get(ch);
fout.put(ch);
}while(ch!=EOF);
fout.close();
Output :
In the above example, statement 1 will create a file named demo.txt in write mode. Statement 2
is do-while loop, which take characters from user and write the character to the file, until user
press the ctrl+z to exit from the loop.
ifstream obj;
obj.get(char &ch);
The get() function reads a single character from the file and puts that value in ch.
void main()
{
ifstream fin;
char ch;
fin.open("demo.txt"); //Statement 1
cout<<"\nData in file...";
while(!fin.eof()) //Statement 2
{
fin.get(ch);
cout<<ch;
}
fin.close();
Output :
Data in file...
Hello friends, my name is kumar.
In the above example, Statement 1 will open an existing file demo.txt in read mode and
statement 2 will read all the characters one by one upto EOF(end-of-file) reached. The eof()
function is member of ifsream class. It returns zero, if end-of-file reached and returns non-zero
value, if any character found.
2. Reading or writing formatted I/O using insertion operator ( << ) and extraction
operator ( >> ).
ofstream obj;
obj << variable-list;
Example of Insertion operator ( << )
#include<fstream.h>
#include<conio.h>
void main()
{
ofstream fout;
int roll;
char name[25];
float marks;
char ch;
fout.open("demo.txt"); //Statement 1
do
{
cout<<"\n\nEnter Roll : ";
cin>>roll;
fout << roll << " " << name << " " << marks << " ";
//Statement 2
}while(ch=='y' || ch=='Y');
fout.close();
}
Output :
Enter Roll : 1
Enter Name : Kumar
Enter Marks : 78.53
Enter Roll : 2
Enter Name : Sumit
Enter Marks : 89.62
Do you want to add another data (y/n) : n
In the above example, Statement 1 will open an existing file demo.txt in write mode and
statement 2 will write all the data in file.
Note : We must insert blank space in file to separate each values, as shown in statement 2,
because extraction operator terminates at blank space and consider next value as new value.
ifstream obj;
obj >> variable-list;
#include<fstream.h>
#include<conio.h>
void main()
{
ifstream fin;
int roll;
char name[25];
float marks;
char ch;
fin.open("demo.txt"); //Statement 1
cout<<"\n\tData in file...\n";
while(fin>>roll>>name>>marks) //Statement 2
{
cout << "\n\t" << roll << "\t" << name << "\t" << marks;
}
fin.close();
}
Output :
Data in file...
1 Kumar 78.53
2 Sumit 89.62
In the above example, Statement 1 will open an existing file demo.txt in read mode and
statement 2 will read all the data upto EOF(end-of-file) reached.
fstream fout;
fout.write( (char *) &obj, sizeof(obj) );
#include<fstream.h>
#include<conio.h>
class Student
{
int roll;
char name[25];
float marks;
void getdata()
{
cout<<"\n\nEnter Roll : ";
cin>>roll;
public:
void AddRecord()
{
fstream f;
Student Stu;
f.open("Student.dat",ios::app|ios::binary);
Stu.getdata();
f.close();
}
};
void main()
{
Student S;
char ch='n';
do
{
S.AddRecord();
} while(ch=='y' || ch=='Y');
Output :
Enter Roll : 1
Enter Name : Ashish
Enter Marks : 78.53
Enter Roll : 2
Enter Name : Kaushal
Enter Marks : 72.65
Enter Roll : 3
Enter Name : Vishwas
Enter Marks : 82.65
fstream fin;
fin.read( (char *) &obj, sizeof(obj) );
#include<fstream.h>
#include<conio.h>
class Student
{
int roll;
char name[25];
float marks;
void putdata()
{
cout<<"\n\t"<<roll<<"\t"<<name<<"\t"<<marks;
public:
void Display()
{
fstream f;
Student Stu;
f.open("Student.dat",ios::in|ios::binary);
cout<<"\n\tRoll\tName\tMarks\n";
f.close();
}
};
void main()
{
Student S;
S.Display();
}
Output :