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 के चरण)
- Include the header file
#include<fstream>. - Create an object of
ofstream,ifstream, orfstream. - Open the file using the
open()method or constructor. - Perform read/write operations.
- 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)
- Which header file is required for file handling in C++?
(a) <iostream> | (b) <fstream> | (c) <stdio.h> | (d) None - What does
ios::outmode do?
(a) Opens file for reading | (b) Opens file for writing | (c) Appends data to the file | (d) None - Which method is used to close a file?
(a) stop() | (b) terminate() | (c) close() | (d) None - What is the default mode for
ofstream?
(a) Read | (b) Write | (c) Append | (d) None - What happens if you open a non-existing file in
ios::inmode?
(a) File is created | (b) Error occurs | (c) Opens in write mode | (d) None - Which class is used to read from a file?
(a) ofstream | (b) ifstream | (c) fstream | (d) None - 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 - Which mode is used for binary file operations?
(a) ios::app | (b) ios::binary | (c) ios::out | (d) None - Which stream class can perform both input and output operations?
(a) ofstream | (b) ifstream | (c) fstream | (d) None - 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
- What is file handling in C++?
Answer: File handling refers to reading from and writing to files for data storage. - Write the syntax to open a file in append mode.
Answer:fstream file("filename.txt", ios::app); - What is the purpose of the
close()function?
Answer: It is used to close an open file and release resources. - What happens if a file opened in
ios::inmode does not exist?
Answer: An error occurs as the file cannot be opened. - 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
- Explain file handling in C++ with examples of reading and writing files.
- Discuss the different file modes available in C++ and their applications.
- Write a program to append data to a file and then read the entire content.
Post a Comment