C++ में File handling और File handling Operations क्या है Program सहित
File handling in C++ in Hindi
C++ में File handling का अर्थ Files के साथ विभिन्न कार्य जैसे File का निर्माण करना, उसे खोलना, पढ़ना, उसमें लिखना और उसे बंद करना है।
यह Program को Computer में संग्रहित File के साथ Interact करने देता है, ताकि Data को सुरक्षित किया जा सके तथा बाद में उसे Use किया जा सकें।
C++, File handling के लिए कई Classes प्रदान करता है जिसमे सबसे आम Classess ifstream, ofstream तथा fstream हैं जो क्रमश: Files से पढ़ने, File पर लिखने और Files में पढ़ने और लिखने दोनों के लिए Use किए जाते हैं।
ये Classes आपको कुछ अन्य कार्य जैसे यह Check करना कि File मौजूद है या नहीं, File से Data पढ़ने या उसमें नया Data को सुरक्षित करने देता है।
File handling का बड़ा फायदा यह है कि यदि Program चलना बंद भी हो जाए तब भी यह Data को रखता है। उदाहरण के लिए, User जो type करता है उसे आप File में सुरक्षित रख सकते हैं और अगली बार Program चलने पर आप उसे वापस पढ़ सकते हैं।
File handling, कोई Error जैसे File ठीक से क्यों नहीं खुल पा रही है इसे भी पता करने में मदद करती है जिससे आपका Program अच्छी तरह से कार्य करता है तथा Crash नही होता।
कुल मिलाकर, File handling, C++ में Programming का एक महत्वपूर्ण हिस्सा है, खासकर जब आपके Program को समय के साथ Data को प्रबंधित और संग्रहीत करने की आवश्यकता होती है।
File handling operations in C++ in Hindi
C++ में File operations में कई कार्य जैसे File को Create करना, Open करना, Read करना लिखना और उसे Close करना शमिल है।
आपके Computer पर संग्रहित Data को handle करने के लिए ये Operations आवश्यक है।
C++, File handling Operations को बड़ी कुशलता के साथ करने के लिए विशिष्ट Classes और Functions प्रदान करता है।
C++ में कुछ Basic operations निम्न है
File को Create करना और उसमे Write करना
C++ में File का निर्माण करने और लिखने के लिए ofstream class, का Use किया जाता है जो
<fstream> का एक भाग है
Program for Creating and Writing to a File
#include<iostream>
#include<fstream>
using namespace std;
int main() {
// Creating and opening a file("demo.txt")
ofstream file("demo.txt");
file<<"Hello world!";//Writing to a File
return 0;
}
Explanation:
1) Headers को शामिल करना:
#include <iostream>:
इसे Input और Output functions के लिए Use किया जाता है।
#include <fstream>`:
Files के साथ कार्य करने के लिए इसकी जरूरत होती है
2) File को Open करना:
ofstream file ("demo.txt");
यह statement एक नया file "demo.txt" नाम से बनाता है और उसमें Write करने के लिए उसे Open कर देता है।
3) File में लिखना:
`file<<"Hello world!";
यह File में Hello world! Sentence को लिखता है।
4) File स्वत: ही Close हो जाता है:
आपको File को Manually बंद करने की जरूरत नहीं है जब प्रोग्राम समाप्त होता है तब वह स्वत: ही बंद हो जाता है
Program for Creating and Writing file by using Error checking
#include<iostream>
#include<fstream>//Need for file handling
using namespace std;
int main() {
// Creating and opening a file("myfile.txt")
ofstream file("myfile.txt");
// Check if the file has been successfully opened.
if (myfile.is_open())
{
// Write to the file
file<< "Good Morning"<<endl;
file<< "Welcome to computerehub"<<endl;
// After writing, close the file.
myfile.close();
cout << "The file was created and written successfully." << endl;
}
else {
// If the file couldn't be opened.
cout << "Unable to open the file." << endl;
}
return 0;
}
Explanation:
1) एक File का निर्माण करना और उसे खोलना:
ofstream file("myfile.txt");
ऊपर के Statement में "myfile.txt" नामक file का निर्माण किया जाता है। यदि इस नाम से कोई file नहीं बनी है तो इसे बनाया जाएगा और यदि है तो यह Open हो जायेगा और पुराना Contents मिट जायेगा।
3) यह Check करना कि क्या File is Open है:
if (myfile.is_open())
यह Check करता है कि क्या file सफलतापूर्वक खोली गई है या नही
4) File में लिखना:
file<< "Welcome to computerehub."<<endl;
File यदि Open हो जाती है तो निम्न Contents को File में लिखा जाता है "Welcome to computerehub."
5) File को Close करना:
myfile.close();
यह Statement File में लिखने के बाद उसे Close करता है
6) Error को Handle करना:
यदि file open नही हो पाता तब प्रोग्राम "Unable to open the file." Message print करेगा।
File को Open करना और उसे Read करना
C++ में एक file को Open और Read करने का मतलब आपके Computer में Store File को Access करना और उसके Contents को प्राप्त करना है,
ताकि Program उस Contents का Use कर सकें या उसे Display कर सकें।
इस कार्य को <fstream> library द्वारा प्रदान किए जा रहे C++ file handling features का Use करके किया जा सकता है।
Program for Opening and Reading File
#include<iostream>
#include<fstream>
#include <string>
using namespace std;
int main() {
// Open a text file for reading
ifstream myfile("myfile.txt");
// Check if the file opened successfully
if (!myfile) {
cout << "Unable to open the file." << endl;
return 1;
}
// Store the contents of the file in "greeting" string
string greeting;
// Loop until the end of the text file
while (getline(myfile, greeting)) {
// Print the greeting variable
cout << greeting << endl;
}
// Closing the file
myfile.close();
return 0;
}
Explanation:
1) एक File को Open करना:
ifstream myfile("myfile.txt");
"myfile.txt" , File को Open करता है।
2)Check करना कि क्या File Open हुआ:
if(!myfile)
यह Check करता है कि क्या File Open हुआ। यदि File open नही होता है तो निम्न Message print होगा
" Unable to open the file."
3) File के Content को store करने के लिए एक Variable का निर्माण:
इसमें एक string type variable 'greeting' का निर्माण किया जाता है ताकि इसमें File के Contents को Store किया जा सकें।
string greeting;
4) While loop चलाना:
while (!myfile.eof())
File के Contents को greeting Variable में एक एक करके Write करने के लिए एक Loop चलाया जाता है जो तब तक चलेगा जब तक End of file तक न पहुंच जाएं या Condition True रहे
3) File को Read करना:
(getline(myfile, greeting))
ऊपर मे Loop चलता जायेगा और myfile के contents को एक समय मे एक Line पढ़कर gretting variable में Store किया जाएगा।
4) Variable में Store Contents को Read करना:
cout << greeting << endl;
greeting, Variable में Store contents को Read किया जाता है।
5) File को Close करना:
myfile.close();
अन्त में यह Statement file को Close कर देता है।
File को Append करना
C++ में एक file को Append करने का मतलब पहले से मौजूद File के Data को Alter किए बिना File के अंत में नया Data Add करना है।
यह तब उपयोगी है जब आप File में पहले से उपस्थित Contents को बिना Overwrite किए, उसे नई सूचनाओं के साथ Update करना चाहते है।
Program for Append to a File
#include<iostream>
#include<fstream>
using namespace std;
int main() {
// opening a text file to append
ofstream file ("myfile.txt", ios::app);
// if the file doesn't open, print an error message
if(!myfile) {
cout << "Unable to open the file to append." << endl;
return 1;
}
// Append multiple sentences to the file
file << "computerehub is a eesite" << endl;
file << "It has computer study material" << endl;
file << "You will get benefits from our site " << endl;
// closing the file
myfile.close();
return 0;
}
Explaination:
1)File को Append Mode में Open करना:
ofstream file ("myfile.txt", ios::app);
"myfile.txt", File को Append mode(ios::app) में खोला जाता है ताकि File के End में Text add किया जा सके।
2) Error को Handle करना:
if(!myfile)`
यदि File Open नहीं होती तो निम्न Message print होगा
"Unable to open the file to append."
3) Append Text:
file << "computerehub is a site" << endl;
file << "It has computer study material" << endl;
file << "You will get benefits from our site " << endl;
यदि File Open होती है तो ऊपर का Sentences File के अंत मे Append हो जाएगी।
0 टिप्पणियाँ