File operations using C++ file streams fstream, ifstream, ofstream In Hindi
C++ प्रोग्रामिंग लैंग्वेज में फ़ाइल हैंडलिंग डाटा रीड और राइट ऑपरेशन fstream क्लास लाइब्रेरी का यूज़ करके हैंडल किए जाते हैं. जो मौजूदा सेकेंडरी स्टोरेज लोकेशन में फ़ाइलों को रीड करने और उन फाइल में डाटा और इनफार्मेशन को क्रिएट राइट करने के लिए क्लास प्रोवाइड करते है।

The most commonly used file handling stream classes are.
- ifstream – फ़ाइल हैंडलिंग प्रोसेस में यह एक इनपुट फ़ाइल स्ट्रीम होती है. इसका यूज़ फ़ाइल हैंडलिंग में मौजूदा फ़ाइलें से डाटा रीड करने में किया जाता है।
- ofstream – फ़ाइल हैंडलिंग ऑपरेशन में यह एक आउटपुट फ़ाइल स्ट्रीम क्लास लाइब्रेरी होती है. इसका यूज़ नई फ़ाइलें क्रिएट या राइट करने में किया जाता है।
- fstream – फ़ाइल हैंडलिंग टास्क में यह फ़ाइल स्ट्रीम क्लास लाइब्रेरी है. इसका यूज़ फ़ाइल हैंडलिंग में मौजूदा फ़ाइलें से डाटा रीड करने और फाइल में डाटा और इनफार्मेशन राइट करने में किया जाता है।
यहाँ ऊपर दी गई ये क्लास C++ प्रोग्रामर को फ़ाइल ऑपरेशन परफॉर्म करने में हेल्प करती हैं. जैसे, फ़ाइल हैंडलिंग में फ़ाइलें ओपन करना, फाइल से डेटा और इनफार्मेशन को रीड करना और डाटा क्रिएट करना या लिखना, मौजूदा फाइल में एक्टिविटीज की जाँच करना और अंत में फ़ाइलें बंद करना आदि।
Below is an overview of how to use these classes for file handling operations in C++.
Using the <fstream> class library.
C++ यूजर फ़ाइल हैंडलिंग प्रोसेस में फ़ाइल स्ट्रीम क्लास लाइब्रेरी को अपने प्रोग्राम में यूज़ करने के लिए, <fstream> हेडर फ़ाइल या प्री-प्रोसेसर के रूप में टॉप में ऐड करें।
#include <iostream>
#include <fstream> // इसे फ़ाइल स्ट्रीम में फाइल रीड और राइट फ़ाइल हैंडलिंग टास्क ऑपरेशन के लिए यूज़ करे
#include <string> // इसे फ़ाइल हैंडलिंग प्रोसेस में स्ट्रिंग डाटा टाइप के साथ काम करने के लिए यूज़ करे
using namespace std;
Writing to a file (using the ofstream class method).
फ़ाइल हैंडलिंग प्रोसेस में किसी टेक्स्ट या बाइनरी फ़ाइल में डेटा क्रिएट या राइट करने के लिए, C++ यूजर ofstream (आउटपुट फ़ाइल स्ट्रीम) क्लास का यूज़ कर सकते हैं। यदि आपके सिस्टम में फ़ाइल मौजूद नहीं है, तो यह पहले उसे क्रिएट करेगा। यदि फाइल मौजूद है, तो यह उसके कंटेंट और इनफार्मेशन को ओवरराइट कर देता है. जब तक C++ यूजर इसमें अपेंड फाइल हैंडलिंग मोड का यूज़ नहीं करते है।
Example of basic file handling (writing to a file).
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// here we create an ofstream object and open the file in write mode purpose only
ofstream outFile(“samplefile.txt”);
// here it Check if the file is open writing purpose
if (outFile.is_open()) {
outFile << “Welcome to Vcanhelpsu” << endl; // here we start writing data and text to the file
outFile << “Let create content in samplefile text.” << endl;
outFile << “Let we learn file handling in detail” << endl;
outFile.close(); // here we close the file after writing operation
cout << “Samplefile Data written successfully.” << endl;
} else {
cout << “Restrict to open file.” << endl;
}
return 0;
}
Explanation of basic file handling (writing to a file).
- यहाँ इस एक्साम्प्ल में ofstream outFile(“samplefile.txt”) “samplefile.txt” यह मौजूदा स्टोरेज लोकेशन में samplefile नाम की एक फाइल को क्रिएट करता है, और राइट मोड में फाइल को ओपन करता है।
- outFile << “data” यह मौजूदा फ़ाइल में डेटा को क्रिएट करता है।
- outFile.close() यह फाइल हैंडलिंग में मौजूदा फाइल में नई डाटा और इनफार्मेशन में लिखने के बाद फ़ाइल को क्लोज कर देता है।
याद रहे, यहाँ यदि आपके सिस्टम में फ़ाइल मौजूद है, तो यह फाइल में एक नया डेटा लिखने से पहले उसे छोटा यानि उसे (एम्प्टी) कर देता है। इस प्रोसेस से बचने के लिए, फाइल हैंडलिंग में फ़ाइल ओपन करते समय ios::app फ़्लैग (append मोड) का यूज़ करें।
Reading from a file in file handling (using the ifstream class method).
फाइल हैंडलिंग में मौजूदा फ़ाइल से डेटा रीड करने के लिए, ifstream (इनपुट फ़ाइल स्ट्रीम) क्लास का यूज़ करते है। जिसमे फ़ाइल डिफ़ॉल्ट रूप से रीड मोड में ओपन होती है।
Example of basic file handling (reading from a file).
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// here we create an ifstream object and open the file in read mode purpose only
ifstream inFile(“samplefile.txt”);
// here it check if the file is open or not
if (inFile.is_open()) {
string line;
// here it read each line from the file
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 purpose only
} else {
cout << “Restrict to open file.” << endl;
}
return 0;
}
Explanation of basic file handling (reading from a file).
- यहाँ इस एक्साम्प्ल में ifstream inFile(“samplefile.txt”) मौजूदा फ़ाइल को रीड मोड में ओपन करता है।
- यह getline(inFile, line) फ़ाइल से एक बार में एक लाइन को रीड करता है।
- इसमें फाइल टेक्स्ट लाइन लूप तब तक चलता रहता है, जब तक की सभी फाइल लाइनें पूरी तरह से रीड नहीं की जातीं है।
Reading and writing to the same file in file handling (using fstream class methods).
जब C++ यूजर को फाइल हैंडलिंग में एक ही फ़ाइल में रीड और राइट दोनों फाइल हैंडलिंग ऑपरेशन परफॉर्म करने हों, तो C++ प्रोग्रामर fstream क्लास लाइब्रेरी मेथड का यूज़ कर सकते हैं।
Example of file handling (reading and writing with fstream).
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// here it open the file for both reading and writing purpose only
fstream file(“samplefile.txt”, ios::in | ios::out);
if (!file) {
cout << “Display Error work with file.” << endl;
return 1;
}
// here it read data from the existing file
string line;
cout << “Start Reading from file.” << endl;
while (getline(file, line)) {
cout << line << endl; // here it print the line to the console screen
}
// here it write new data to the active file
file.clear(); // here it clear EOF flag
file.seekp(0, ios::end); // here it move to the end of the file for appending mode purpose
file << “Let append or add new data to the file.” << endl;
file.close(); // here it close the file after operation
cout << “File Data written to successful.” << endl;
return 0;
}
Explanation of file handling (reading and writing with fstream).
- यहाँ इस एक्साम्प्ल में fstream file(“samplefile.txt”, ios::in | ios::out) फ़ाइल को इनपुट और आउटपुट दोनों फाइल मोड रीड और राइट ऑपरेशन के लिए ओपन करता है।
- यह फाइल हैंडलिंग में getline(file, line) फ़ाइल से टेक्स्ट लाइन्स को रीड करता है।
- यह फाइल हैंडलिंग में file.clear() EOF फ़्लैग को क्लियर करता है, जिससे कि हम मौजूदा फाइल में लिखना कंटिन्यू कर सकते है।
- फाइल हैंडलिंग में file.seekp(0, ios::end) मौजूदा फाइल में नया डेटा अपेण्ड ऐड करने के लिए राइट फाइल पॉइंटर को फ़ाइल के अंत में मूव करता है।
File modes for fstream, ifstream, and ofstream in file handling.
फाइल हैंडलिंग में मौजूदा फ़ाइल को ओपन करते समय C++ यूजर अलग-अलग फ़ाइल मोड को इंडीकेट कर सकते हैं। ये फाइल हैंडलिंग मोड फाइल को कंट्रोल करने में हेल्प करते हैं. जिससे कि मौजूदा फ़ाइल को कैसे ओपन की जाए और फाइल में डेटा को कैसे एक्सेस और मैनेज किया जाए।
Popular file handling modes are.
- ios::in – फाइल हैंडलिंग मोड में फ़ाइल को रीड करने के लिए ओपन करे।
- ios::out – फाइल हैंडलिंग मोड में फ़ाइल को राइट लिखने के लिए ओपन करे. (यदि इसमें फ़ाइल मौजूद नहीं है, तो यह पहले उसे क्रिएट करता है, यदि फाइल मौजूद है. तो फ़ाइल को छोटा यानि उसे एम्प्टी कर देता है।
- ios::app – फाइल हैंडलिंग मोड में फ़ाइल को एपेंड मोड में ओपन करे. यानि एक नया डेटा फ़ाइल के अंत में क्रिएट या लिखा जाता है।
- ios::ate – फाइल हैंडलिंग मोड में फ़ाइल डाटा क्रिएट करने के लिए ओपन करे और तुरंत फाइल के अंत में कर्सर को मूव करे।
- ios::binary – फाइल हैंडलिंग मोड में फ़ाइल को बाइनरी मोड में आपने करे. यानि एक नॉन-टेक्स्ट फ़ाइलों के साथ काम करने के लिए इसे यूज़ करे।
- ios::trunc – यह फाइल हैंडलिंग में यदि फ़ाइल पहले से मौजूद है, तो यह उसे ज़ीरो लेंथ तक छोटा करता है, यह ofstream के लिए डिफ़ॉल्ट मेथड होता है।
Example of file handling (opening a file in different modes).
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// here it open a file for writing and truncate purpose default file mode
ofstream outFile1(“samplefile.txt”);
// here it open a file for appending purpose only data written at the end of existing file
ofstream outFile2(“datafile.txt”, ios::app);
// here it open a file for reading and writing purpose only without truncating method
fstream outFile3(“information.txt”, ios::in | ios::out | ios::app);
// here it open a file in binary mode machine language mode
ofstream outFile4(“binarydata.bin”, ios::binary);
return 0;
}
Explanation of file handling (opening a file in different modes).
- यह फाइल हैंडलिंग मोड ofstream outFile1(“samplefile.txt”) टेक्स्ट इनफार्मेशन को राइट लिखने के लिए फ़ाइल को ओपन करता है. यह फाइल कंटेंट को छोटा यानि एम्प्टी कर देता है।
- यह फाइल हैंडलिंग मोड ofstream outFile2(“datafile.txt”, ios::app) फाइल के अंत में डेटा और इनफार्मेशन को अपेण्ड करने के लिए फ़ाइल को ओपन करता है।
- यह फाइल हैंडलिंग मोड fstream outFile3(“information.txt”, ios::in | ios::out | ios::app) फ़ाइल को रीड और राइट लिखने दोनों के लिए एपेंड मोड में ओपन करता है।
- यह फाइल हैंडलिंग मोड ofstream outFile4(“binarydata.bin”, ios::binary) बाइनरी फ़ाइल को राइट लिखने के लिए बाइनरी मोड में ओपन करते है।

