Class 11-12

Class 11 Computer Science

BSEB 100-Day Study Plan
Notes, MCQs, and Solutions

Explore Class 11

Class 12 Computer Science

Advanced Topics & Practical Guide
Board Exam Preparation

Explore Class 12

Day 47: File Handling in C++ – Basics, Modes, and Examples | Class 11

Learn file handling in C++ with Day 47 of Class 11 Computer Science. Covers basics, file modes, and examples with practical outputs.

Day 47 – File Handling in C++

Class 11 Computer Science

What is File Handling? (File Handling क्या है?)

File handling in C++ refers to the process of reading from and writing to files using classes provided by the standard library. It allows data storage in secondary memory for long-term use.
C++ में File Handling का तात्पर्य फाइलों से डेटा पढ़ने और लिखने की प्रक्रिया से है। यह डेटा को लंबे समय तक उपयोग के लिए द्वितीयक मेमोरी में संग्रहीत करने की अनुमति देता है।

Types of File Handling in C++ (C++ में File Handling के प्रकार)

  • Text Files: Stores data in human-readable format.
    टेक्स्ट फाइल: डेटा को मानव-पठनीय प्रारूप में संग्रहीत करता है।
  • Binary Files: Stores data in binary format (machine-readable).
    बाइनरी फाइल: डेटा को बाइनरी प्रारूप में संग्रहीत करता है।

File Handling Modes in C++ (C++ में File Handling Modes)

C++ provides several file modes to specify how a file should be opened.
C++ विभिन्न फाइल मोड प्रदान करता है जो दर्शाते हैं कि फाइल को कैसे खोला जाना चाहिए।

  • ios::in – Open file for reading.
  • ios::out – Open file for writing.
  • ios::app – Open file for appending data at the end.
  • ios::binary – Open file in binary mode.

Steps for File Handling in C++ (C++ में File Handling के चरण)

  1. Include the header file #include<fstream>.
  2. Create an object of ofstream, ifstream, or fstream.
  3. Open the file using the open() method or constructor.
  4. Perform read/write operations.
  5. Close the file using the close() method.

Advantages of File Handling (File Handling के लाभ)

  • Enables permanent storage of data.
    डेटा का स्थायी भंडारण सक्षम करता है।
  • Allows reading and writing of large data sets efficiently.
    बड़े डेटा सेट को कुशलता से पढ़ने और लिखने की अनुमति देता है।
  • Facilitates sharing and reusing data.
    डेटा को साझा करने और पुनः उपयोग करने की सुविधा देता है।

Examples of File Handling in C++ (C++ में File Handling के उदाहरण)

Example 1: Writing to a File

#include <fstream>
#include <iostream>
using namespace std;

int main() {
    ofstream file("example.txt"); // Create and open a file
    if (file.is_open()) {
        file << "Welcome to File Handling in C++!";
        file.close(); // Close the file
        cout << "Data written to file successfully!";
    } else {
        cout << "Unable to open file!";
    }
    return 0;
}

Output:

Data written to file successfully!

Example 2: Reading from a File

#include <fstream>
#include <iostream>
using namespace std;

int main() {
    ifstream file("example.txt"); // Open the file for reading
    if (file.is_open()) {
        string line;
        while (getline(file, line)) {
            cout << line << endl;
        }
        file.close(); // Close the file
    } else {
        cout << "Unable to open file!";
    }
    return 0;
}

Output:

Welcome to File Handling in C++!

Example 3: Appending Data to a File

#include <fstream>
#include <iostream>
using namespace std;

int main() {
    fstream file("example.txt", ios::app); // Open the file in append mode
    if (file.is_open()) {
        file << "\nAppending new data to the file.";
        file.close(); // Close the file
        cout << "Data appended successfully!";
    } else {
        cout << "Unable to open file!";
    }
    return 0;
}

Output:

Data appended successfully!

Practice Questions

Multiple Choice Questions (MCQs)

  1. Which header file is required for file handling in C++?
    (a) <iostream> | (b) <fstream> | (c) <stdio.h> | (d) None
  2. What does ios::out mode do?
    (a) Opens file for reading | (b) Opens file for writing | (c) Appends data to the file | (d) None
  3. Which method is used to close a file?
    (a) stop() | (b) terminate() | (c) close() | (d) None
  4. What is the default mode for ofstream?
    (a) Read | (b) Write | (c) Append | (d) None
  5. What happens if you open a non-existing file in ios::in mode?
    (a) File is created | (b) Error occurs | (c) Opens in write mode | (d) None
  6. Which class is used to read from a file?
    (a) ofstream | (b) ifstream | (c) fstream | (d) None
  7. What is the output of the following code?
    ofstream file("data.txt", ios::app);
    file << "Hello, World!";
    file.close();
            
    (a) Writes "Hello, World!" to a new file | (b) Appends "Hello, World!" to the file | (c) Deletes existing content | (d) None
  8. Which mode is used for binary file operations?
    (a) ios::app | (b) ios::binary | (c) ios::out | (d) None
  9. Which stream class can perform both input and output operations?
    (a) ofstream | (b) ifstream | (c) fstream | (d) None
  10. What is the purpose of the getline() function?
    (a) To read a single character | (b) To read a line of text | (c) To read binary data | (d) None

Answers to MCQs:

1: (b), 2: (b), 3: (c), 4: (b), 5: (b), 6: (b), 7: (b), 8: (b), 9: (c), 10: (b)

Short Answer Questions

  1. What is file handling in C++?
    Answer: File handling refers to reading from and writing to files for data storage.
  2. Write the syntax to open a file in append mode.
    Answer:
    fstream file("filename.txt", ios::app);
    
  3. What is the purpose of the close() function?
    Answer: It is used to close an open file and release resources.
  4. What happens if a file opened in ios::in mode does not exist?
    Answer: An error occurs as the file cannot be opened.
  5. Write a program to write and read data from a file in C++.
    Answer:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main() {
        ofstream file("data.txt");
        file << "File Handling in C++";
        file.close();
    
        ifstream readFile("data.txt");
        string content;
        getline(readFile, content);
        cout << content;
        readFile.close();
    
        return 0;
    }
    

Long Answer Questions

  1. Explain file handling in C++ with examples of reading and writing files.
  2. Discuss the different file modes available in C++ and their applications.
  3. Write a program to append data to a file and then read the entire content.

Post a Comment