C++ File I/O


File handling is an important part of all programs. Most of the applications will have their own features to save some data to the local disk and read data from the disk again. C++ File I/O classes simplify such file read/write operations for the programmer by providing easier to use classes.

C++ File I/O Classes and Functions:

There are 3 File I/O classes in C++ which are used for File Read/Write operations. They are

  • ifstream         -  Can be used for File read/input operations
  • ofstream        -  Can be used for File write/output operations
  • fstream          -  Can be used for both read/write c++ file I/O operations

The most important methods which will be used for any file operations are:

  1. fstream::open method – to open the file
  2. fstream::Operator >> and fstream::Operator <<  – For reading from or writing to the file.
  3. fstream::close  – Flushes all buffer and close the file

Reading a text file using fstream class:
There are several ways of reading the text from a file. But all of them have a common approach as follows.

  1. Open the file
  2. Read the data
  3. Close the file

This sample code snippet explains how to use the c++ file i/o stream operators to read data from a file. In all cases the header file fstream.h must be included.


#include<fstream.h>
int main()
{
char str[2000];
fstream file_op("c:\test_file.txt",ios::in);
while(file_op >> str)
cout << str ;

file_op.close();

return 0;
}

The class fstream, which is used above is the one which is commonly used for c++ file i/o manipulations. The constructor of fstream takes 2 parameters. One is the file path and the second is the File Open mode. There are several open modes, each of them with a different purpose. Some of them are ios::in for reading, ios::out for writing, ios::app for appending to the end of file, ios::binary for opening in binary mode etc.,
Now for the purpose of this article, as the data is read from the file, the flag ios::in is used. After this,  the read operation is continued till the end of the file. The while loop ensures a continuous read till the end of the file or it encounters any abnormal break. If the program is built and run , it displays all the data read from the file. The C++ File I/O read job is done.
But if we look at the output closely, there is a draw back in using this stream operator read. The output misses the white spaces and the end of line characters. In order not to miss these characters we can either use fstream::get() or fstream::getline() methods. Here is the example for using fstream getline method.


 
#include <fstream.h>
int main()
{
char str[2000];
fstream file_op("c:\test_file.txt",ios::in);
while(!file_op.eof())
{
file_op.getline(str,2000);
cout <<str;
}        file_op.close();
cout <<endl;

Writing to a text file using fstream class:
Writing to a text file can also be achieved with the stream operators. This also follows the same order of operations, though with a slight difference.
1. open a file – in write mode
2. Write to a file
3. close the file
Look at the following sample code to see the difference.


#include <fstream.h>
int main()
{
fstream file_op("c:\CoderSource_file.txt",ios::out);

file_op<<"Test Write to file";
file_op.close();
return 0;
}

To modify the data or to seek to a different position inside the file, the c++ file i/o class fstream provides member functions like seekg() etc., These functions can be used to relocate the record insert position to the desired locations.
After all the C++ File I/O operations we do a fstream::close(), to close the file pointer. This is not mandatory. Even if this function is not called by the application, the destructor of the fstream class will close the file when the object goes out of scope.