Reading and writing to files c++ In Hindi
C++ प्रोग्रामिंग लैंग्वेज इसके यूजर को अपनी बिल्ट-इन fstream क्लास लाइब्रेरी के माध्यम से टेक्स्ट और बाइनरी फ़ाइलों को हैंडल और मैनेज करने की परमिशन प्रोवाइड करते है. जिसमें यूजर डिफाइन C++ प्रोग्राम फ़ाइलें रीड और राइट करने के लिए कई क्लास इन्क्लुड होती हैं. पोपुलर फाइल हैंडलिंग ऑपरेशन में मुख्य फाइल ifstream (इनपुट फ़ाइल स्ट्रीम), ofstream (आउटपुट फ़ाइल स्ट्रीम), और fstream (इनपुट और आउटपुट दोनों) टास्क के लिए यूज़ होती है। ये फाइल क्लास लाइब्रेरी C++ यूजर को मौजूदा प्रोग्राम में फ़ाइलें को ओपन करने, फाइल रीड करने, फाइल टेक्स्ट क्रिएट करने, और फाइल क्लोज करने के परमिशन प्रोवाइड करते हैं।

An overview of the file streams concept in C++ programming.
- ifstream (input file stream) – मौजूदा C++ प्रोग्राम फ़ाइलों से डेटा रीड करने के लिए ifstream क्लास लाइब्रेरी का यूज़ होता है।
- ofstream (output file stream) – मौजूदा C++ प्रोग्राम फ़ाइलों में डेटा क्रिएट लिखने के लिए ofstream क्लास लाइब्रेरी का यूज़ होता है
- ।fstream (file stream) – मौजूदा C++ प्रोग्राम फ़ाइलों में रीड और राइट दोनों फाइल हैंडलिंग ऑपरेशन के लिए इसका यूज़ होता है।
Adding the <fstream> header file for file handling task.
C++ प्रोग्रामिंग लैंग्वेज में फाइल हैंडलिंग टास्क के माध्यम से फ़ाइलों के साथ काम करने के लिए, C++ यूजर को <fstream> प्री-प्रोसेसर हेडर फाइल को जरूर ऐड करना होगा।
#include <iostream>
#include <fstream> // फ़ाइल हैंडलिंग स्ट्रीम ऑपरेशन के लिए इसे ऐड करे
#include <string> // स्ट्रिंग डाटा टाइप ऑपरेशन के साथ काम करने के लिए इसे ऐड करे
using namespace std;
File Handling: Writing data to a file (using the ofstream class).
C++ प्रोग्राम फ़ाइल में डाटा और इनफार्मेशन को लिखने के लिए, C++ यूजर ofstream क्लास लाइब्रेरी का यूज़ कर सकते हैं। इससे आप फ़ाइल को राइट मोड में ओपन कर सकते हैं, और उसमें डिजायर डेटा और इनफार्मेशन क्रिएट कर सकते हैं।
Basic example of writing data to a file.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// here we Create an ofstream object and open the file for operation
ofstream outFile(“samplefile.txt”);
// here it Check if the file opened successfully or not
if (outFile.is_open()) {
outFile << “Welcome to Vcanhelpsu” << endl; // here it Writing text info into the file
outFile << “We use sample file.” << endl;
outFile << “let learn file handling in c++ program file” << endl;
outFile.close(); // here it Close the file after writing data and info
cout << “Data information added successfully in c++ program file.” << endl;
} else {
cout << “Unable to open or write data into the file.” << endl;
}
return 0;
}
Explanation of a basic of writing data to a file.
- ofstream outFile(“samplefile.txt”) – यह “samplefile.txt” नाम की एक फ़ाइल क्रिएट करता है, और उस फाइल को राइट मोड में ओपन करता है।
- outFile << “data” – यह फ़ाइल में डेटा और इनफार्मेशन को क्रिएट करता है।
- outFile.close() – यह samplefile.txt फाइल में डाटा लिखने के बाद फ़ाइल को क्लोज कर देता है।
याद रहे, यदि यहाँ आपके सिस्टम में फ़ाइल मौजूद नहीं है, तो यह पहले उसे क्रिएट करेगा। यदि फ़ाइल पहले से मौजूद है. तो पिछला लिखा गया फाइल कंटेंट अपने आप नए कंटेंट से ओवरराइट हो जाएगा।
Reading from a file in file handling (using the ifstream file method).
C++ प्रोग्राम फ़ाइल में फ़ाइल से डाटा और टेक्स्ट इनफार्मेशन को रीड करने के लिए, C++ यूजर ifstream फाइल मेथड क्लास का यूज़ कर सकते हैं। यह मेथड C++ यूजर को फाइल हैंडलिंग ऑपरेशन्स में मौजूदा फ़ाइल ओपन करने और उस फाइल का कंटेंट रीड करने की परमिशन प्रोवाइड करता है।
Reading from a file in a basic example of file handling.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// here we create an ifstream file object and open the file for reading from file
ifstream inFile(“samplefile.txt”);
// here it check if the file opened successfully for file operation
if (inFile.is_open()) {
string line;
// here it read each line from the file for reading purpose
while (getline(inFile, line)) {
cout << line << endl; // here it print the line to the console screen
}
inFile.close(); // here it close the file after reading operation
} else {
cout << “Restrict to open file for reading purpose.” << endl;
}
return 0;
}
Explanation of reading from a file in file handling.
- ifstream inFile(“samplefile.txt”) – यह मौजूदा फाइल को रीड मोड में “samplefile.txt” फाइल को क्रिएट करता और फिर उसे ओपन करता है।
- getline(inFile, line) – यह फाइल हैंडलिंग टास्क में फ़ाइल से एक बार में एक लाइन टेक्स्ट इनफार्मेशन को स्ट्रिंग लाइन में रीड करता है। इस प्रोसेस में प्रोग्राम लूप तब तक चलता रहता है, जब तक की मौजूदा फाइल की सभी लाइनें कम्पलीट पढ़ नहीं ली जातीं है।
- inFile.close() – यह मौजूदा फाइल पढ़ने के बाद फाइनली फ़ाइल को क्लोज कर देता है।
Reading and writing with the fstream library in file handling (for both input and output tasks).
C++ यूजर फाइल हैंडलिंग ऑपरेशन में fstream क्लास लाइब्रेरी मेथड का यूज़ तब भी कर सकते हैं, जब C++ यूजर को किसी फ़ाइल से डाटा और इनफार्मेशन को रीड करने और उसमें न्यू डाटा और इनफार्मेशन को लिखने दोनों फाइल टास्क की एक साथ ज़रूरत हो। fstream क्लास को स्पेशली तब यूज़ किया जाता है, जब C++ यूजर को मौजूदा फ़ाइलों को अपडेट या मॉडिफाई करने की जरूरत हो।
Basic example of reading and writing with the fstream.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// here we Create an fstream file object and open the file in both input and output file operation mode
fstream file(“samplefile.txt”, ios::in | ios::out | ios::app);
// here it Check if the file opened successfully or not
if (file.is_open()) {
string line;
// here it Read from the samplefile file
cout << “Reading data and information from samplefile text file.” << endl;
while (getline(file, line)) {
cout << line << endl; // here it Print each line by line
}
// here it Move the file pointer back to the beginning for writing file text purpose
file.clear(); // here it Clear EOF flag
file.seekp(0, ios::end); // here it Move to the end for appending file mode
// here it Write to the file
file << “Here we add a new line with fstream class library.” << endl;
file.close(); // here it Close the file after reading and writing operation purpose
cout << “Data written and update to the samplefile text file successfully.” << endl;
} else {
cout << “Restricted to open samplefile text file for operation.” << endl;
}
return 0;
}
Explanation of reading and writing files with the fstream.
- fstream file(“example.txt”, ios::in | ios::out | ios::app) – यह मौजूदा फ़ाइल से डाटा और इनफार्मेशन को रीड करने (ios::in) और फाइल डाटा लिखने के लिए (ios::out) और दोनों फाइल हैंडलिंग ऑपरेशन के लिए, और नया डाटा अपेण्ड करने के लिए अपेण्ड मोड (ios::app) में फाइल को ओपन करता है।
- file.clear() – यह मौजूदा फाइल से डाटा पढ़ने के बाद सेट किए गए किसी भी EOF फ़्लैग को क्लियर करता है. जिससे मौजूदा फाइल में डाटा इनफार्मेशन सीकिंग और राइटिंग जैसे आगे के ऑपरेशन इजीली इनेबल हो जाते हैं।
- file.seekp(0, ios::end) – यह राइट फाइल पॉइंटर को फ़ाइल के अंत में ले जाता है, जिससे कि मौजूदा फाइल कंटेंट को ओवरराइट करने के बजाय नया फाइल डेटा को अपेण्ड किया जा सके।
Multiple File Operation Modes in C++ File Handling.
C++ फाइल हैंडलिंग ऑपरेशन में फ़ाइलें को ओपन करते समय, C++ यूजर अलग-अलग फ़ाइल हैंडलिंग मोड जिसमे (फ़्लैग का यूज़ करके) यह इंडीकेट कर सकते हैं। ये फाइल ऑपरेशन मोड कंट्रोल करते हैं कि फ़ाइल को कैसे खोली जाए और कौन से फाइल टास्क ऑपरेशन अलाउड हैं।
Type of Multiple File Operation Modes in C++.
- ios::in – फाइल हैंडलिंग ऑपरेशन में किसी फाइल को रीड करने के लिए ओपन करे।
- ios::out – फाइल हैंडलिंग ऑपरेशन में फाइल में डाटा इनफार्मेशन क्रिएट लिखने के लिए ओपन करे. यदि सिस्टम में फ़ाइल मौजूद नहीं है, तो यह पहले उसे क्रिएट करता है, या यदि मौजूद है, तो यह फ़ाइल को छोटा कर देता है।
- ios::app – फाइल हैंडलिंग ऑपरेशन में किसी फाइल में नया कंटेंट और इनफार्मेशन को ऐड करने के लिए ओपन करे (सभी राइट ऑपरेशन फ़ाइल के अंत में ऐड किए जाते हैं)।
- ios::ate – इसमें फाइल हैंडलिंग ऑपरेशन में फाइल लिखने के लिए खोलें और तुरंत फ़ाइल के आखिर में ले जाता है।
- ios::binary – यह फाइल हैंडलिंग ऑपरेशन में फ़ाइल को बाइनरी मोड में ओपन करता है, यह टेक्स्ट मोड के रिवर्स काम करता है।
- ios::trunc – फाइल हैंडलिंग ऑपरेशन में यदि फाइल सिस्टम में पहले से मौजूद है, तो उसे ज़ीरो लेंथ तक ट्रंकेट करता है।
Example Using file handling modes in different modes.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// here it Open file in binary mode and for writing data information purpose only
ofstream outFile(“binary_file.bin”, ios::binary);
if (outFile.is_open()) {
int data = 1031110;
outFile.write(reinterpret_cast<char*>(&data), sizeof(data)); // here it Writing an integer data to a binary file format
outFile.close();
cout << “Data information written successfully to the binary file.” << endl;
}
// here it Open the binary file for reading purpose only
ifstream inFile(“binary_file.bin”, ios::binary);
if (inFile.is_open()) {
int readFileData;
inFile.read(reinterpret_cast<char*>(&readFileData), sizeof(readFileData)); // here it Reading data from a binary file
cout << “Binary file Read data operation perform sucessfully. ” << readFileData << endl;
inFile.close();
}
return 0;
}
Explaining how to use different file handling modes.
- ios::binary – यह फाइल हैंडलिंग में फिक्स करता है कि फ़ाइल बाइनरी मोड में ही ओपन हो।
- outFile.write(reinterpret_cast<char*>(&data), sizeof(data)) – यह बाइनरी फाइल हैंडलिंग में फ़ाइल में इंटीजर डेटा का बाइनरी रिप्रेजेंटेशन को क्रिएट करता है।
- inFile.read(reinterpret_cast<char*>(&readData), sizeof(readData)) – यह फाइल हैंडलिंग में बाइनरी फाइल डेटा को वापस readFileData में रीड करता है।
Error handling with file operations in file handling.
फाइल हैंडलिंग में मल्टीप्ल फाइल डीलिंग मोड फ़ाइलो के साथ काम करते समय, C++ यूजर को फाइल एरर को प्रॉपर आर्डर में मैनेज और हैंडल करना चाहिए। यह C++ फाइल हैंडलिंग में फ़ाइल ऑपरेशन सफल हुआ या नहीं है. यह चेक करने के लिए C++ यूजर fail() फ़ंक्शन को अप्लाई कर सकते हैं।
Example with error handling in file handling.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outFile(“samplefile.txt”);
if (!outFile) {
cout << “Display Error – Restrict to open samplefile for writing purpose only” << endl;
return 1;
}
outFile << “Welcome to Vcanhelpsu platform” << endl;
if (outFile.fail()) {
cout << “Display Error – Writing to the samplefile task failed” << endl;
return 1;
}
outFile.close();
cout << “Samplefile Data written operation successfully to the file.” << endl;
return 0;
}
Explaining error handling in file handling.
- यहाँ इस एक्साम्प्ल में if (!outFile) फंक्शन यह चेक करता है कि मौजूदा फ़ाइल ओपन नहीं जा सकी या मौजूद नहीं है।
- यह if (outFile.fail()) फंक्शन यह चेक करता है कि मौजूदा फाइल हैंडलिंग फ़ाइल में डाटा और इनफार्मेशन को लिखते समय कोई एरर तो नहीं डिस्प्ले हुआ.

