- 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 को रखता है।
File handling operations in C++ in Hindi - C++ में फाइल हैंडलिंग ऑपरेशन
File को Create करना और उसमे Write करना
#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;
}
#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 (file.is_open())
{
// Write to the file
file<< "Good Morning"<<endl;
file<< "Welcome to computerehub"<<endl;
// After writing, close the file.
file.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;
}
File को Open करना और उसे Read करना
#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;
}
File को Append करना
#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 website" << endl;
file << "It has computer study material" << endl;
file << "You will get benefits from our site " << endl;
// closing the file
myfile.close();
return 0;
}
Related Posts
> C++ क्या है? उसके इतिहास, गुण, उपयोग, फायदे और नुकसान
> Basic structure of C++ Program
> C++ में Tokens क्या है? और उसके प्रकार
> C++ Variables क्या है?, उसके प्रकार, उसे कैसे Declare, Define करते हैं
> C++ में Constants क्या है? उसके प्रकारों की संपूर्ण जानकारी
> C++ में Basic Input और Output (cin,cout,cerr) की जानकारी
> Data type in C++ की संपूर्ण जानकारी
> C+ में Operators और उसके प्रकार जानें Practical सहित
> C++ में Conditional और उसके प्रकारों को जानें Practical सहित
> C++ में Looping statements और उसके प्रकार Practical सहित
> C++ में Jump Statements और उसके प्रकारों की संपूर्ण जानकारी Practical सहित
> C++ में Array क्या है? और उसके प्रकारों की जानकारी Practical सहित
> C++ में Function क्या है उसके प्रकार, उपयोग प्रोग्राम सहित
> C++ में Structure क्या है Practical सहित
> OOPs Concepts in C++ in Hindi- C++ में OOPs के बारे में
> Oops के फायदे और नुकसान की जानकारी
> C++ में Class और Object की सम्पूर्ण जनकारी
> C++ में Array of Objects क्या है?
> C++ में Pointers, Pointer to an objects, Pointer to an Array की संपूर्ण जानकारी हिंदी में।
> C++ में Passing objects क्या है
> C++ में Reference और Type Casting की संपूर्ण जानकारी
> C++ में Access specifier की संपूर्ण जानकारी
> C++ में Static Data Members और Member Functions के बारे में Practical सहित
> C++ में Memory allocation और Memory management operators (new और delete) Practical सहित
> Friend Function in C++ in Hindi
> Friend Class in C++ in Hindi Practical सहित
> Inline function in C++ in Hindi
> Function Overloading in C++ in Hindi Practical सहित
> Operator Overloading in C++ in Hindi PRACTICAL सहित
> C++ में Constructor क्या है और उसके प्रकारों की संपूर्ण जानकारी
> C++ में Destructor क्या है ?उसकी संपूर्ण जानकारी
> C++ मे Inheritance क्या है उसके प्रकारों को जानें प्रोग्राम सहित
> C++ में Polymorphism क्या है? और उसके प्रकारों को जानें
> C++ में Virtual function की संपूर्ण जानकारी
> C++ में Exception handling की संपूर्ण जानकारी
0 टिप्पणियाँ