File Handling in C++
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
File handling in C++
File handling lets a program save data to disk and read it back. C++ uses the <fstream> library with three classes: ofstream (write), ifstream (read), and fstream (both). They work just like cout and cin, but on files.
Writing to a file
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream out("data.txt"); // open for writing
out << "Hello file\n";
out << "Second line\n";
out.close(); // close when done
cout << "Written.\n";
return 0;
}Written.
(data.txt now contains the two lines)
Reading from a file
ifstream in("data.txt"); // open for reading
string word;
while (in >> word) { // read word by word
cout << word << " ";
}
in.close();Reading line by line
Use getline to read whole lines, which keeps spaces:
ifstream in("data.txt");
string line;
while (getline(in, line)) {
cout << line << "\n";
}
in.close();File open modes
| Mode | Meaning |
|---|---|
ios::in | Open for reading |
ios::out | Open for writing (clears file) |
ios::app | Append to the end |
ios::trunc | Truncate existing content |
ios::binary | Binary mode |
ofstream out("log.txt", ios::app); // add without erasingChecking for errors
Always confirm the file opened before using it:
ifstream in("missing.txt");
if (!in) { // or in.is_open()
cout << "Could not open file\n";
return 1;
}Common mistakes
- Not checking whether the file opened before reading/writing.
- Forgetting to
close()(or relying on scope to close it). - Using
ios::outwhen you meantios::app, wiping the file. - Mixing
>>(stops at spaces) withgetlinewithout clearing the newline.
Write a program that appends a new line to notes.txt each run, then reads the whole file back and prints it with line numbers. Handle the case where the file does not yet exist.
Summary
- File handling uses
<fstream>:ofstream,ifstream,fstream. - Write with
<<, read with>>orgetline. - Open modes like
ios::appandios::binarycontrol behaviour. - Always check the file opened before using it.
- Close files when finished.
Frequently Asked Questions
How do you handle files in C++?
<fstream> library, which provides three stream classes: ofstream for writing, ifstream for reading, and fstream for both. You open a file through one of these objects, use stream operators to read or write, and close it when done.What is the difference between ifstream, ofstream, and fstream in C++?
ifstream is an input file stream used only for reading, ofstream is an output file stream used only for writing, and fstream can do both reading and writing on the same file. You choose based on whether you need to read, write, or both.How do you read a file line by line in C++?
ifstream and call getline(file, line) inside a loop, which reads one line at a time into a string until the end of the file. This is the standard way to process a text file line by line.What are file open modes in C++?
ios::in for reading, ios::out for writing, ios::app to append, ios::trunc to clear existing content, and ios::binary for binary data. You can combine them with the bitwise OR operator when opening a file.How do you check if a file opened successfully in C++?
if (!file) or if (!file.is_open()). If the check fails the file could not be opened — perhaps it does not exist or there is no permission — and you should handle that before reading or writing.C++ में file handling
File handling program को data disk पर सहेजने और वापस पढ़ने देता है। C++ <fstream> library को तीन classes के साथ इस्तेमाल करता है: ofstream (लिखना), ifstream (पढ़ना), और fstream (दोनों)। ये बिल्कुल cout और cin जैसे काम करते हैं, पर files पर।
File में लिखना
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream out("data.txt"); // likhne ko kholein
out << "Hello file\n";
out << "Second line\n";
out.close(); // kaam poora hone par band karein
cout << "Written.\n";
return 0;
}Written.
(data.txt me ab do lines hain)
File से पढ़ना
ifstream in("data.txt"); // padhne ko kholein
string word;
while (in >> word) { // shabd-dar-shabd padhein
cout << word << " ";
}
in.close();पंक्ति-दर-पंक्ति पढ़ना
पूरी पंक्तियाँ पढ़ने को getline इस्तेमाल करें, जो spaces रखता है:
ifstream in("data.txt");
string line;
while (getline(in, line)) {
cout << line << "\n";
}
in.close();File open modes
| Mode | अर्थ |
|---|---|
ios::in | पढ़ने को खोलें |
ios::out | लिखने को खोलें (file साफ़ करता है) |
ios::app | अंत में append करें |
ios::trunc | मौजूदा सामग्री truncate करें |
ios::binary | Binary mode |
ofstream out("log.txt", ios::app); // mitaye bina jodeinErrors जाँचना
इस्तेमाल से पहले हमेशा पुष्टि करें कि file खुली:
ifstream in("missing.txt");
if (!in) { // ya in.is_open()
cout << "Could not open file\n";
return 1;
}आम गलतियाँ
- पढ़ने/लिखने से पहले यह न जाँचना कि file खुली।
close()भूलना (या इसे बंद करने को scope पर निर्भर रहना)।- जब
ios::appचाहिए था तबios::outइस्तेमाल करना, file मिटाते हुए। >>(spaces पर रुकता है) कोgetlineके साथ newline साफ़ किए बिना मिलाना।
एक program लिखें जो हर बार चलने पर notes.txt में एक नई पंक्ति append करे, फिर पूरी file वापस पढ़े और line numbers के साथ print करे। उस मामले को संभालें जहाँ file अभी मौजूद नहीं।
सारांश
- File handling
<fstream>इस्तेमाल करता है:ofstream,ifstream,fstream। <<से लिखें,>>याgetlineसे पढ़ें।ios::appऔरios::binaryजैसे open modes व्यवहार नियंत्रित करते हैं।- इस्तेमाल से पहले हमेशा जाँचें कि file खुली।
- काम पूरा होने पर files बंद करें।