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.
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 बंद करें।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में files कैसे संभालते हैं?
<fstream> library से संभालता है, जो तीन stream classes देती है: लिखने को ofstream, पढ़ने को ifstream, और दोनों को fstream। आप इनमें से किसी object के ज़रिए file खोलते हैं, पढ़ने या लिखने को stream operators इस्तेमाल करते हैं, और काम पूरा होने पर इसे बंद करते हैं।C++ में ifstream, ofstream, और fstream में क्या अंतर है?
ifstream input file stream है जो केवल पढ़ने को इस्तेमाल होता है, ofstream output file stream है जो केवल लिखने को, और fstream एक ही file पर पढ़ना और लिखना दोनों कर सकता है। आप इस आधार पर चुनते हैं कि आपको पढ़ना, लिखना, या दोनों चाहिए।C++ में file को पंक्ति-दर-पंक्ति कैसे पढ़ते हैं?
ifstream से खोलते हैं और एक loop के अंदर getline(file, line) call करते हैं, जो file के अंत तक एक बार में एक पंक्ति एक string में पढ़ता है। यह text file को पंक्ति-दर-पंक्ति process करने का मानक तरीका है।C++ में file open modes क्या हैं?
ios::in, लिखने को ios::out, append को ios::app, मौजूदा सामग्री साफ़ करने को ios::trunc, और binary data को ios::binary। आप file खोलते समय इन्हें bitwise OR operator से जोड़ सकते हैं।C++ में कैसे जाँचें कि file सफलतापूर्वक खुली?
if (!file) या if (!file.is_open()) से। अगर जाँच विफल हो तो file नहीं खुल सकी — शायद यह मौजूद नहीं या अनुमति नहीं — और आपको पढ़ने या लिखने से पहले इसे संभालना चाहिए।