The fstream Library in C++
C++ provides file handling using fstream in C++ through the header, enabling file input output in C++.
| Class | Purpose | Direction |
|---|
| ifstream | Input file stream — reads from files | Read Only |
| ofstream | Output file stream — writes to files | Write Only |
| fstream | File stream — reads and writes | Both |
#include
#include // Required for file handling in C++
using namespace std;
Opening & Closing Files in C++
Before any file operations in C++, you must open the file.
Method 1 — via constructor:
ofstream outFile("data.txt"); // opens on object creation
Method 2 — via open() function:
ofstream outFile;
outFile.open("data.txt"); // explicit open
Always close the file when done:
outFile.close(); // releases the file handle
Not closing a file can lead to data loss. This is an important concept in C++ file handling best practices.

Writing to a File in C++
Use ofstream and the insertion operator << to write data to a file in C++, similar to cout.
#include
#include
using namespace std;
int main() {
ofstream outFile("students.txt");
if (!outFile) {
cout << "Error: Could not open file." << endl;
return 1;
}
outFile << "Name: Alice" << endl;
outFile << "Roll No: 101" << endl;
outFile << "Marks: 95" << endl;
outFile.close();
cout << "Data written successfully." << endl;
return 0;
}
Output in students.txt: Name: Alice | Roll No: 101 | Marks: 95 — each on a new line

Reading from a File in C++
Use ifstream to read file in C++.
#include
#include
#include
using namespace std;
int main() {
ifstream inFile("students.txt");
string line;
if (!inFile) {
cout << "Error: File not found." << endl;
return 1;
}
while (getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
return 0;
}
You can also read word by word:
string word;
while (inFile >> word) {
cout << word << " ";
}
File Open Modes in C++
C++ allows control over file behavior using file open modes in C++.
| Mode Flag | Meaning | Use Case |
|---|
| ios::in | Open for reading | Read-only access |
| ios::out | Open for writing (truncates) | Overwrite file content |
| ios::app | Append to end of file | Add logs without overwriting |
| ios::ate | Start at end of file | Random write at end |
| ios::trunc | Truncate file to zero length | Clear existing file |
| ios::binary | Open in binary mode | Binary file handling in C++ |
ofstream logFile("log.txt", ios::app);
logFile << "[INFO] Server started successfully.\n";
logFile.close();
fstream file("data.bin", ios::in | ios::out | ios::binary);
Binary File Handling in C++
Binary file handling in C++ is used for efficient storage of structured data.
#include
using namespace std;
struct Student {
char name[50];
int rollNo;
float marks;
};
int main() {
Student s = {"Alice", 101, 95.5f};
ofstream out("student.bin", ios::binary);
out.write(reinterpret_cast<char*>(&s), sizeof(s));
out.close();
Student s2;
ifstream in("student.bin", ios::binary);
in.read(reinterpret_cast<char*>(&s2), sizeof(s2));
in.close();
cout << s2.name << " | " << s2.rollNo << " | " << s2.marks;
return 0;
}
Binary files are faster and more precise than text files in C++ file handling.
Error Handling in C++ File Handling
Always check whether a file was opened successfully before performing operations.
ifstream file("config.txt");
if (!file.is_open()) {
cerr << "Error: File could not be opened." << endl;
return 1;
}
Other checks:
fail()
bad()
eof()
Summary of File Handling in C++
| Topic | Key Point |
|---|
| Header file | #include |
| Write to file | Use ofstream with << |
| Read from file | Use ifstream with getline() or >> |
| Read & Write | Use fstream |
| Append data | Use ios::app |
| Binary data | Use ios::binary |
| Always close | Call close() |
| Check errors | Use is_open(), fail(), bad(), eof() |
Conclusion
The ability to handle files is crucial in C++ programming. From simple logging systems to enterprise applications, mastering file handling in C++ with examples is essential for development, projects, and technical interviews.
