C++ File Handling

Sometimes a program needs to store information so that it can be used later, even after the program has been closed. Variables only keep data while the program is running. To store data permanently, C++ allows you to work with files.

C++ File Handling is used to create, read, write, and update files. C++ provides the <fstream> library for working with files.

The three main classes used for file handling are ifstream, ofstream, and fstream.

ifstream in C++

ifstream is used to read data from a file. The name comes from “input file stream.”

First, include the <fstream> header:

#include <fstream>

Example

ifstream file("data.txt");

string text;

while (getline(file, text))
{
    cout << text << endl;
}

file.close();

Here, data.txt is opened for reading. getline() reads the file line by line.

You can also check whether the file was opened successfully:

if (!file)
{
    cout << "File could not be opened";
}

ofstream in C++

ofstream is used to write data to a file.

ofstream file("data.txt");

file << "Hello C++";
file.close();

If data.txt does not exist, C++ will normally create it.

You can write multiple values to the file:

ofstream file("student.txt");

file << "Name: Aman" << endl;
file << "Marks: 85" << endl;

file.close();

The information is stored in the file instead of being displayed on the screen.

fstream in C++

fstream can be used for both reading and writing files. This makes it useful when a program needs to perform both operations on the same file.

fstream file("data.txt", ios::in | ios::out);

string text;

getline(file, text);

file << "New Data";

file.close();

The ios::in mode opens the file for reading, while ios::out allows writing.

This is the basic idea behind File Handling in C++ using fstream.

Text Files

A text file stores information in a form that can normally be read by people. Files such as .txt and .csv are common examples.

You can write text using ofstream:

ofstream file("notes.txt");

file << "C++ File Handling";
file << endl;
file << "Text file example";

file.close();

To read it:

ifstream file("notes.txt");

string line;

while (getline(file, line))
{
    cout << line << endl;
}

file.close();

Text files are convenient when the stored information needs to be readable or edited manually.

Binary Files in C++

Binary Files in C++ store data in binary form rather than normal readable text. They are useful when working with data that needs to be stored in its raw form.

You can open a binary file using ios::binary.

For example:

ofstream file("data.dat", ios::binary);

int number = 100;

file.write((char*)&number, sizeof(number));

file.close();

To read the value:

ifstream file("data.dat", ios::binary);

int number;

file.read((char*)&number, sizeof(number));

cout << number;

file.close();

Output

100

Binary files are commonly used when storing structured data efficiently.

Random Access Files

Normally, when reading a file, you move through the data from the beginning. Sometimes you need to move directly to a particular position. This is called random access.

C++ provides seekg() and seekp() for this purpose.

  • seekg() moves the get pointer, used for reading.
  • seekp() moves the put pointer, used for writing.

For example:

file.seekg(10);

This moves the reading position to a particular location in the file.

You can also find the current position using tellg() and tellp().

These functions are especially useful when working with large files or fixed-size records where you need to access specific data without reading everything before it.

File Opening Modes

C++ provides several modes for opening files:

ModePurpose
ios::inRead from a file
ios::outWrite to a file
ios::appAdd data at the end
ios::binaryOpen as a binary file
ios::truncRemove existing contents

You can combine modes using the | operator.

File handling becomes much easier once you understand which stream and opening mode to use. ifstream is mainly for reading, ofstream for writing, and fstream when you need both.


Frequently Asked Questions (FAQs)

Q1. How do I open a file for reading in C++?

Use ifstream to open and read data from a file. You can associate it with a filename and then read its contents using input operations.

Q2. What is the difference between ifstream, ofstream, and fstream?

ifstream is mainly used for reading, ofstream for writing, and fstream can be used for both reading and writing files.

Q3. How are text and binary files different in C++?

Text files store data in a human-readable format, while Binary Files in C++ store data as raw bytes, which can be more efficient for certain types of data.

Q4. Can C++ read and write binary files?

Yes. C++ supports binary file operations using streams such as ifstream, ofstream, and fstream with the appropriate file-opening mode.

Q5. What is random access in C++ file handling?

Random access allows you to move directly to a specific position in a file instead of reading it sequentially from the beginning. It is useful when working with large files or specific records.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top