Files in C++

Files are used to store data permanently in a storage device. In C++, file handling is a mechanism to store the output of a program in a file and to perform various operations on it.

The fstream Library

C++ provides the fstream library that has capabilities to create, read, and write to a file. The fstream library is provided with three classes:

  • ofstream - This class represents an output file stream. It is used for creating files and writing information to files.
  • ifstream - This class represents an input file stream. It is used for reading information from data files.
  • fstream - This class represents both ofstream and ifstream. It provides the capability of creating, reading, and writing to a file.

File Operations in C++

C++ provides the following capabilities to perform operations on files:

  • open - To be able to perform any operations on a file, it must be opened first.
  • read - It allows us to read data from a file.
  • write - It allows us to write data to a file.
  • close - We should always close the file after completing the operations on a file.

Opening a File

To perform any operation on a file, first we have to open a file.

Syntax

open(FileName, Mode);

There are different modes to open a file:

Mode Description
iso::in Opens a file in reading mode.
iso::out Opens a file in writing mode.
iso::app Opens a file in append mode.
iso::ate Opens a file in append mode but read and write is performed at the end of the file.
iso::binary Opens a file in binary mode.
iso::trunc opens a file in truncate mode.
iso::nocreate Opens a file only if the file exists.
iso::noreplace Opens a file only if the file doesn't exist.
Example

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream file;
    file.open("example.txt");
    return 0;
}

Reading a File

We can read information from files using the stream extraction operator >>.

Let the following be the file example.txt:

Hello, This is an example file.

Following is the code to read the content of the above example.txt file:


#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream new_file;
    new_file.open("example.txt", ios::in);
    if (!new_file)
    {
        cout << "File doesn't exist.";
    }
    else
    {
        char i;
        while (1)
        {
            new_file >> i;
            if (new_file.eof())
                break;
            cout << i;
        }
    }
    new_file.close();
    return 0;
}
Output
Hello, This is an example file.

Writing to a File

The stream insertion operator << to write to a file. The text written to the file should be enclosed within the double-quotes.

Example

#include<iostream>                         
#include<fstream>               
             
using namespace std;

int main() {
    fstream new_file;                       
    new_file.open("example.txt", ios::out);                
    if (!new_file) {                            
        cout<<" Error while creating the file.";          
    }
    else {
        cout<<"New file created";    
        new_file <<"Hello!, this will be written to the file.";  
        new_file.close();                   
    }
    return 0;
}

The above code will create a new file example.txt with text "Hello!, this will be written to the file." in it.

Closing a File

When we are done with our input and output operations on a file, we should close the file with the help of close() function.

Syntax

my_filename.close();
Example

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream new_file;
    new_file.open("example.txt", ios::out);
    new_file.close();
    return 0;
}