⬅ Previous Next ➡

File Handling in C++

File Handling in C++
  • File Handling in C++ is done using stream classes from <fstream>.
  • Main classes:
    • ofstream - write to file
    • ifstream - read from file
    • fstream - read + write both
  • Files can be handled as text files or binary files.

1) File Streams (ifstream, ofstream, fstream)

  • ofstream creates/writes file.
  • ifstream opens/reads file.
  • fstream supports both reading and writing.
  • Always check if file opened successfully using is_open().

2) Writing to a Text File (ofstream)

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

int main() {
    ofstream out("note.txt");

    if (!out.is_open()) {
        cout << "File open failed!" << endl;
        return 0;
    }

    out << "Hello File!" << endl;
    out << "C++ File Handling Demo" << endl;

    out.close();
    cout << "Data written successfully." << endl;

    return 0;
}

3) Reading from a Text File (ifstream)

  • Use getline() to read line by line.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ifstream in("note.txt");

    if (!in.is_open()) {
        cout << "File not found!" << endl;
        return 0;
    }

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

    in.close();
    return 0;
}

4) Using fstream (Read + Write)

  • Use ios::in for reading and ios::out for writing.
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    fstream file;
    file.open("data.txt", ios::out);

    if (!file.is_open()) return 0;

    file << "Line 1" << endl;
    file << "Line 2" << endl;
    file.close();

    file.open("data.txt", ios::in);
    string s;
    while (getline(file, s)) {
        cout << s << endl;
    }
    file.close();

    return 0;
}

5) File Modes (ios:: modes)

  • ios::in - open for reading
  • ios::out - open for writing
  • ios::app - append (write at end)
  • ios::ate - move to end at open
  • ios::trunc - truncate (clear file)
  • ios::binary - binary mode

6) Append Mode Example

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

int main() {
    ofstream out("note.txt", ios::app);

    if (!out.is_open()) return 0;

    out << "Appended line..." << endl;
    out.close();

    return 0;
}

7) Binary File Handling

  • Binary files store data in raw form (faster, no formatting).
  • Use write() and read() functions.
7.1) Write Object to Binary File
#include <iostream>
#include <fstream>
using namespace std;

class Student {
public:
    int id;
    char name[20];
};

int main() {
    Student s = {1, "Sourav"};

    ofstream out("stud.dat", ios::binary);
    if (!out.is_open()) return 0;

    out.write((char*)&s, sizeof(s));
    out.close();

    cout << "Binary data saved." << endl;
    return 0;
}
7.2) Read Object from Binary File
#include <iostream>
#include <fstream>
using namespace std;

class Student {
public:
    int id;
    char name[20];
};

int main() {
    Student s;

    ifstream in("stud.dat", ios::binary);
    if (!in.is_open()) return 0;

    in.read((char*)&s, sizeof(s));
    in.close();

    cout << "ID: " << s.id << endl;
    cout << "Name: " << s.name << endl;

    return 0;
}

8) Quick Notes

  • ofstream = write, ifstream = read, fstream = both.
  • Use ios::app for append, ios::trunc to clear file.
  • Use ios::binary + read/write for binary files.
  • Always close files after use (or rely on destructor at end of scope).
⬅ Previous Next ➡