File Handling in CPP
File Hling in CPP
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.
C++ provides the <fstream>
library which includes three main classes for file operations:
Class | Purpose |
---|---|
ifstream | Read data from files (input) |
ofstream | Write data to files (output) |
fstream | Both read and write |
To use these classes, include the header:
#include <fstream>
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.
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
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
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;
}
Mode | Description |
---|---|
ios::in | Open file for reading |
ios::out | Open file for writing (overwrite) |
ios::app | Append new data to end of file |
ios::binary | Open file in binary mode |
ios::ate | Open and move to end of file |
You can combine modes using bitwise OR |
.
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.
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.