C++ file handling

What is file handling?

In C++ file handling is a very important topic , file handling provides us the facility of saving our data into secondary memory.

Whenever we give input / Data, that data stores into primary memory (RAM) through the variable and stay there till the termination of the program , once program is terminated the data of the variable deleted from the memory automaticaly and we can`t access that data again. Because on the time of execution program stores data into (Primary memory (RAM) with the help of variables but when we get exit from our program that data is automatically deleted from the primary memory.

For get rid from this problem C++ provides the concept of file handling by which we can save our program`s data into secondary memory (HDD) of our system , this process of saving our data permanently is known as file handling.

With the help of file handling we can store our data into a particular location of our secondary memory (HDD) , and we can access that data any time for update it delete it or for perform any other actions on it.

 

Note.

A variable`s life can`t be more than the life of program , once the program is terminated the life of variable will be finish with the termination of program.

 

Let`s start with file handling.

For start with file handling in C++ , we need to include fstream library , this library contains all useful functions of file handling for performing different – different types of operations with files.
Let`s see how to add fstream library.

#include<fstream.h>

So , this is how you simply need to add fstream.h header file in your program.

 

Reading and writing a file.

C++ programming provides three main predefined classes for perform actions on a file.

  • ofstream – used to output file stream.
  • ifstream – used for input file stream.
  • fstream – used for both input stream and output stream.

How to create the object of fstream classes.

For performing any operation with files , first we have to create the objects of fstream classes.

 

Syntax.

class_name object_name;

Example.

ofstream offile;  //output.
ifstream iffile;  //input.
fstream iofile;  //input output both.

In above example ofstream , ifstream and fstream are the predefined classes for perform actions over the input / output files , And offile , iffile and iofile are the objects of the classes.

 

Steps to achieve File Handling.

For achieving a file in C++ we need to follow some steps are given below.

  • Opening the file.
  • Reading data from file.
  • writing data into file.
  • Closing the file.

File handling modes.

Modes Used to.
ios :: in this mode is used Open a file for input.
ios :: out this mode is used to open a file for reading.
ios :: app this mode is used to append all output to the end of the file.
ios :: binary this mode is used to read of write binary files.
ios :: ate This mode is used to take the file pointer to the end of the file , but can write form anywhere into a file.
ios :: trunc This mode is used to delete the content of the file before opening the file.

 

Writing data into a file.

In this example we are going to write data into file. So that we can retrieve that data when needed.

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;

int main()
{
 ofstream offile; //Creating object of ofstream class.
 offile.open("myfile.text");
 offile<<"I am the content of file.";
 cout<<"Data written to file";
 offile.close();
 return 0;
 getch();
}

Output.

Data written to file.

 

Reading data from a file.

Files store data so it may be retrieved for processing when needed. The previous example demonstrated how to write data into a file. Now we discuss that how to read the data of a file.

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;

int main()
{
 char read[100];
 ifstream iffile;
 iffile.open("myfile.text");
 cout<<"Reading from file."<<endl;
 while(!iffile.eof())
 {
  iffile.getline(read,100);
  cout<<read;
 }
 cout<<endl<<"Reading is done";
 iffile.close();
 return 0;
 getch();
}

Output.

Reading form file.
I am the content of file.
Reading is done.

 

Reading and writing data from a file.

In this example we are going to perform the both actions , writing the data into a file and after that we are going to read / retrieve that data from the file.

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;

int main()
{
 char read[100];
 ofstream offile; //Creating object of ofstream class.
 offile.open("myfile.text");
 offile<<"I am the line into file."<<endl;
 offile<<"I am the second line into file."<<endl;
 cout<<"Data written to file";
 ifstream iffile;
 iffile.open("myfile.text");
 cout<<endl<<"Reading from the file."<<endl;
 while(!iffile.eof())
 {
 iffile.getline(read,100);
 cout<<endl<<endl<<read;
 }
 offile.close();
 iffile.close();
 return 0;
 getch();
}

Output.

Data written to file.
Reading from the file.
I am the line into file.
I am the second line into file.

 

Functions of file handling.

Functions Use
tellg() Provides the current location of the get pointer.
tellp() Provides the current location of file pointer.
seekg() Moves the cursor at desired location , inside a file.