× back Opening a file Closing a file Writing to a file Reading from a file Programs
Next Topic → ← Previous Topic

I/O system basics, File I/O

Opening a file

Mode flag and description

  • ios::app
    • Append mode. All output to that file to be appended to the end.
  • ios::ate
    • Open a file for output and move the read/write control to the end of the file.
  • ios::in
    • Open a file for reading.
  • ios::out
    • Open a file for writing.
  • ios::trunc
    • If the file already exists, its contents will be truncated before opening the file.

We can combine two or more of these values by ORing them together. For example if you want to open a file in write mode and want to truncate it in case that already exists, following will be the syntax:

                    
ofstream outfile
outfile.open("file.dat", ios::out | ios::trunc);
                    
                
Similar way, you can open a file for reading and writing purpose as follows:
            
fstream afile;
afile.open("file.dat", ios::out | ios::in );
            
        

Closing a file

                
#include <fstream>
using namespace std;
int main() {
    fstream file("abc.dat", ios::in | ios::out); // Open the file for input/output

    // Perform operations on the file

    // Close the file
    file.close();

    return 0;
}
                
            

Writing to a file

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

int main()
{
    ofstream outputFile("output.txt", ios::out | ios::ate); // Open the file for writing

    if (outputFile.is_open())
    {                                          // Check if the file is successfully opened
        outputFile << "Hello, World!" << endl; // Write data to the file
        outputFile << "This is a sample text." << endl;
        outputFile.close(); // Close the file
        cout << "Data written to the file." << endl;
    }
    else
    {
        cout << "Failed to open the file." << endl;
    }

    return 0;
}
                   
               

Reading from a file

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

int main()
{
    ifstream inputFile("input.txt"); // Open the file for reading

    if (inputFile.is_open())
    { // Check if the file is successfully opened
        string line;
        while (getline(inputFile, line))
        {                         // Read each line from the file
            cout << line << endl; // Display the line on the console
        }
        inputFile.close(); // Close the file
    }
    else
    {
        cout << "Failed to open the file." << endl;
    }

    return 0;
}
                   
               

Example programs

Program in which we read data from one file and write it into some other file

                       
#include <fstream>
using namespace std;

int main()
{
    int a, b, c;
    string inputFileName = "input.txt";
    string outputFileName = "output.txt";

    ifstream fin;
    fin.open(inputFileName);
    fin >> a >> b;
    c = a + b;

    ofstream fout;
    fout.open(outputFileName);
    fout << c << endl;

    fin.close();
    fout.close();

    return 0;
}
                       
                   

Make sure that you have already created a input.txt file and put any two number separated by space or newline.

                       
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    char data[100];
    ofstream outfile; // open a file in write mode
    outfile.open("afile.txt");
    cout << "Writing to the file " << endl;
    cout << "Enter your name : ";
    cin.getline(data, 100); // cin >> data;
    // write inputted data into the file
    outfile << data << endl;
    cout << "Enter you age : ";
    cin >> data;

    // again write inputted data into the file
    outfile << data << endl;
    // close the open file
    outfile.close();

    // open a file in read mode
    ifstream infile;
    infile.open("afile.txt");
    cout << "Reading from the file " << endl;
    infile >> data;
    // write the data at the screen
    cout << data << endl;
    // again read the data from the file and display it
    infile >> data;
    cout << data << endl;
    // close the opened file
    infile.close();
    return 0;
}
                       
                   

Reference