Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🔵 Core C++  ·  Lesson 36

File Handling in C++

Written and reviewed by · 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

C++
#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;
}
Output:
Written.
(data.txt now contains the two lines)

Reading from a file

C++
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:

C++
ifstream in("data.txt");
string line;
while (getline(in, line)) {
    cout << line << "\n";
}
in.close();

File open modes

ModeMeaning
ios::inOpen for reading
ios::outOpen for writing (clears file)
ios::appAppend to the end
ios::truncTruncate existing content
ios::binaryBinary mode
C++
ofstream out("log.txt", ios::app);   // add without erasing

Checking for errors

Always confirm the file opened before using it:

C++
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::out when you meant ios::app, wiping the file.
  • Mixing >> (stops at spaces) with getline without clearing the newline.
🏋️ Practice

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 >> or getline.
  • Open modes like ios::app and ios::binary control 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 में लिखना

C++
#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;
}
Output:
Written.
(data.txt me ab do lines hain)

File से पढ़ना

C++
ifstream in("data.txt");         // padhne ko kholein
string word;
while (in >> word) {              // shabd-dar-shabd padhein
    cout << word << " ";
}
in.close();

पंक्ति-दर-पंक्ति पढ़ना

पूरी पंक्तियाँ पढ़ने को getline इस्तेमाल करें, जो spaces रखता है:

C++
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::binaryBinary mode
C++
ofstream out("log.txt", ios::app);   // mitaye bina jodein

Errors जाँचना

इस्तेमाल से पहले हमेशा पुष्टि करें कि file खुली:

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

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.