File Handling in CPP

9/18/2025

File Hling in CPP

Go Back

File Handling in C++ (With  Example)

Introduction

File handling in C++ is an essential concept that allows developers to store and retrieve data from files on disk. It is especially useful for building applications that require data persistence—such as storing user information, configuration files, or logs.

In this article, we’ll explore file handling in C++ using the fstream library with a real-world Company example.


 File Hling in CPP

File Handling Classes in C++

C++ provides the <fstream> library which includes three main classes for file operations:

ClassPurpose
ifstreamRead data from files (input)
ofstreamWrite data to files (output)
fstreamBoth read and write

To use these classes, include the header:

#include <fstream>

Example Scenario: Developer Writing and Reading Data

Imagine a Developer working in a Company who needs to store developer details (name, role, experience) into a file and then read them back later.

Writing to a File using ofstream

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

int main() {
    ofstream outFile("developer.txt");

    string name = "Alice";
    string role = "Software Engineer";
    int experience = 3;

    if (outFile.is_open()) {
        outFile << "Name: " << name << "\n";
        outFile << "Role: " << role << "\n";
        outFile << "Experience: " << experience << " years\n";
        outFile.close();
        cout << "Developer details saved to file." << endl;
    } else {
        cout << "Error opening file for writing." << endl;
    }
    return 0;
}

Output (in developer.txt):

Name: Alice
Role: Software Engineer
Experience: 3 years

Reading from a File using ifstream

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

int main() {
    ifstream inFile("developer.txt");
    string line;

    if (inFile.is_open()) {
        cout << "Reading developer details from file:\n";
        while (getline(inFile, line)) {
            cout << line << endl;
        }
        inFile.close();
    } else {
        cout << "Error opening file for reading." << endl;
    }
    return 0;
}

Sample Output:

Reading developer details from file:
Name: Alice
Role: Software Engineer
Experience: 3 years

Using fstream for Both Read and Write

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

int main() {
    fstream file("developer.txt", ios::out | ios::in | ios::app);

    if (file.is_open()) {
        file << "Company: TechCorp\n";
        file.seekg(0); // Move cursor to beginning

        string line;
        while (getline(file, line)) {
            cout << line << endl;
        }
        file.close();
    } else {
        cout << "Could not open file." << endl;
    }
    return 0;
}

File Open Modes

ModeDescription
ios::inOpen file for reading
ios::outOpen file for writing (overwrite)
ios::appAppend new data to end of file
ios::binaryOpen file in binary mode
ios::ateOpen and move to end of file

You can combine modes using bitwise OR |.


Best Practices for File Handling

  • Always close files using close() after use.

  • Use is_open() to check if the file opened successfully.

  • Handle exceptions and errors gracefully.

  • Use absolute paths when necessary to avoid confusion.


Conclusion

File handling in C++ makes it easy to store and retrieve data. By using classes like ifstream, ofstream, and fstream, developers can build powerful applications that support persistent data storage. The Developer–Company example shows how real-world information can be saved and read from files with minimal code.