Text File Handling
Creating, writing to, and reading from plain text files in C++.
Text File Handling
Text File Handling का अर्थ computer program की सहायता से text files को create (बनाना), open (खोलना), read (पढ़ना), write (लिखना), append (नई information जोड़ना) और close (बंद करना) है। C++ में file handling का उपयोग data को permanent रूप से store करने के लिए किया जाता है।
What is a Text File?
Text File ऐसी file होती है जिसमें data characters और readable text के रूप में store किया जाता है। Text files को सामान्य text editors की सहायता से खोला और पढ़ा जा सकता है।
उदाहरण के लिए .txt extension वाली file एक सामान्य text file हो सकती है। इसमें names, marks, addresses या अन्य textual information store की जा सकती है।
Why is File Handling Used?
Program में सामान्य variables में रखा data program समाप्त होने के बाद खो सकता है। यदि data को future use के लिए सुरक्षित रखना हो, तो उसे file में store किया जा सकता है।
File handling के प्रमुख उपयोग हैं:
- Data को permanently store करना।
- Stored data को बाद में read करना।
- Existing file में नया data जोड़ना।
- Large amount of data को manage करना।
- Program और external files के बीच data transfer करना।
File Handling in C++
C++ में file handling के लिए <fstream> header file का उपयोग किया जाता है। यह header file file input और output के लिए आवश्यक classes provide करती है।
#include <fstream>
C++ में file handling के लिए मुख्य classes हैं:
| Class | Purpose |
|---|---|
| ofstream | File में data लिखने के लिए |
| ifstream | File से data पढ़ने के लिए |
| fstream | File से data पढ़ने और लिखने दोनों के लिए |
ofstream Class
ofstream का पूरा नाम Output File Stream है। इसका उपयोग file में data लिखने के लिए किया जाता है।
उदाहरण:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream file("student.txt");
file << "Rahul";
file.close();
cout << "Data written successfully";
return 0;
}
Data written successfully
इस program में student.txt नाम की file open/create की जाती है और उसमें Rahul लिखा जाता है।
ifstream Class
ifstream का पूरा नाम Input File Stream है। इसका उपयोग file से data पढ़ने के लिए किया जाता है।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file("student.txt");
string name;
file >> name;
cout << "Name: " << name;
file.close();
return 0;
}
Name: Rahul
यहाँ program student.txt file से data read करता है और उसे name variable में store करता है।
fstream Class
fstream class का उपयोग file में data को read और write दोनों करने के लिए किया जा सकता है।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file("data.txt", ios::out);
file << "Hello C++";
file.close();
cout << "Data saved";
return 0;
}
Data saved
Opening a File
C++ में file को open करने के लिए file stream object का उपयोग किया जाता है। File को constructor या open() function की सहायता से open किया जा सकता है।
Constructor द्वारा file open करने का syntax:
ofstream file("data.txt");
या:
ofstream file;
file.open("data.txt");
Writing Data to a Text File
Text file में data लिखने के लिए ofstream object के साथ insertion operator << का उपयोग किया जा सकता है।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream file("marks.txt");
file << "Physics: 85" << endl;
file << "Chemistry: 90" << endl;
file << "Computer: 95" << endl;
file.close();
cout << "Marks saved successfully";
return 0;
}
Marks saved successfully
इस program के बाद marks.txt file में तीन lines का data store हो जाएगा।
Reading Data from a Text File
Text file से data पढ़ने के लिए ifstream class का उपयोग किया जाता है। एक word या token को पढ़ने के लिए extraction operator >> का उपयोग किया जा सकता है।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file("marks.txt");
string subject;
int marks;
file >> subject >> marks;
cout << subject << ": " << marks;
file.close();
return 0;
}
Physics: 85
यहाँ file से पहला subject और उसके marks read किए जाते हैं।
Reading a Complete Line
यदि file में spaces सहित पूरी line पढ़नी हो, तो getline() function का उपयोग किया जाता है।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file("student.txt");
string line;
getline(file, line);
cout << line;
file.close();
return 0;
}
Rahul Kumar
getline() function spaces सहित पूरी line को read कर सकता है।
Reading File Line by Line
किसी text file की सभी lines को पढ़ने के लिए while loop और getline() का उपयोग किया जा सकता है।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file("student.txt");
string line;
while (getline(file, line))
{
cout << line << endl;
}
file.close();
return 0;
}
Rahul Kumar
Class XII
Computer Science
इस program में file की प्रत्येक line को क्रम से पढ़कर screen पर display किया जाता है।
File Opening Modes
C++ में file को अलग-अलग modes में open किया जा सकता है। File opening mode यह निर्धारित करता है कि file के साथ कौन-सा operation किया जाएगा।
| Mode | Meaning |
|---|---|
| ios::in | File को reading के लिए open करता है। |
| ios::out | File को writing के लिए open करता है। |
| ios::app | Existing file के end में data जोड़ने के लिए उपयोग होता है। |
| ios::ate | File open होने के बाद initial position को end पर set करता है। |
| ios::trunc | Existing content को हटाकर file को नया लिखने के लिए तैयार करता है। |
Append Mode
Append mode का उपयोग existing file के data को हटाए बिना उसके अंत में नया data जोड़ने के लिए किया जाता है। इसके लिए ios::app mode का उपयोग किया जाता है।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream file("student.txt", ios::app);
file << "Amit" << endl;
file.close();
cout << "New data added";
return 0;
}
New data added
इस program में Amit को existing student.txt file के अंत में add किया जाएगा। पुराने data को remove नहीं किया जाएगा।
Checking Whether a File is Open
File operations करने से पहले यह check करना useful होता है कि file successfully open हुई है या नहीं। इसके लिए is_open() function का उपयोग किया जा सकता है।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream file("data.txt");
if (file.is_open())
{
cout << "File opened successfully";
}
else
{
cout << "Unable to open file";
}
file.close();
return 0;
}
File opened successfully
Closing a File
File operation पूरा होने के बाद file को close करना चाहिए। इसके लिए close() member function का उपयोग किया जाता है।
file.close();
File close करने से resources release होते हैं और pending output properly file में write हो सकता है।
Writing Multiple Types of Data
Text file में string के साथ integer, floating-point values और अन्य data भी text के रूप में लिखा जा सकता है।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream file("student.txt");
string name = "Ravi";
int age = 17;
float marks = 89.5;
file << "Name: " << name << endl;
file << "Age: " << age << endl;
file << "Marks: " << marks << endl;
file.close();
cout << "Student data saved";
return 0;
}
Student data saved
File में data लगभग इस प्रकार store होगा:
Name: Ravi
Age: 17
Marks: 89.5
File Handling Process
Text file handling को सामान्यतः निम्न steps में समझा जा सकता है:
- Required header file include करना।
- File stream object create करना।
- File को appropriate mode में open करना।
- File में data read या write करना।
- File operation पूरा होने के बाद file close करना।
Include <fstream> → Open → Read/Write → Close
Difference Between ofstream, ifstream and fstream
| Class | Operation | Common Use |
|---|---|---|
| ofstream | Write | File में data लिखना |
| ifstream | Read | File से data पढ़ना |
| fstream | Read + Write | File से data पढ़ना और लिखना |
Example: Write and Read a Text File
निम्न program में पहले file में data write किया जाता है और फिर उसी file से data read किया जाता है।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outFile("message.txt");
outFile << "Welcome to C++ File Handling";
outFile.close();
ifstream inFile("message.txt");
string message;
getline(inFile, message);
cout << message;
inFile.close();
return 0;
}
Welcome to C++ File Handling
Important Points
- C++ में file handling के लिए <fstream> header file का उपयोग किया जाता है।
- ofstream का उपयोग file में data write करने के लिए किया जाता है।
- ifstream का उपयोग file से data read करने के लिए किया जाता है।
- fstream का उपयोग reading और writing दोनों के लिए किया जा सकता है।
- ios::app existing file के end में data add करने के लिए उपयोग होता है।
- getline() spaces सहित पूरी line read करने के लिए उपयोगी है।
- is_open() से check किया जा सकता है कि file successfully open हुई है या नहीं।
- File operation पूरा होने के बाद close() function का उपयोग करना चाहिए।
- Text files में data readable text form में store किया जाता है।
Board Focus
<fstream> → File Handling Header File
ofstream → Write to File
ifstream → Read from File
fstream → Read + Write
ios::in → Input Mode
ios::out → Output Mode
ios::app → Append Mode
getline() → Complete Line Read करना
is_open() → File Open Status Check करना
close() → File Close करना
Board Important Questions
Very Short Answer Questions
Q1. C++ में file handling के लिए कौन-सी header file उपयोग होती है?
Answer: C++ में file handling के लिए <fstream> header file का उपयोग किया जाता है।
Q2. ofstream क्या है?
Answer: ofstream एक C++ class है जिसका उपयोग file में data लिखने के लिए किया जाता है।
Q3. ifstream क्या है?
Answer: ifstream एक C++ class है जिसका उपयोग file से data पढ़ने के लिए किया जाता है।
Q4. fstream का क्या उपयोग है?
Answer: fstream का उपयोग file से data पढ़ने और file में data लिखने दोनों के लिए किया जा सकता है।
Q5. ios::app का क्या उपयोग है?
Answer: ios::app का उपयोग existing file के अंत में नया data जोड़ने के लिए किया जाता है।
Q6. getline() function का क्या उपयोग है?
Answer: getline() function spaces सहित पूरी line को read करने के लिए उपयोग किया जाता है।
Q7. File को close करने के लिए कौन-सा function उपयोग होता है?
Answer: File को close करने के लिए close() function का उपयोग होता है।
Short Answer Questions
Q8. Text File Handling क्या है?
Answer: Text File Handling का अर्थ program की सहायता से text files को create, open, read, write, append और close करना है। इसका उपयोग data को permanent रूप से store करने और बाद में उसे access करने के लिए किया जाता है।
Q9. ofstream और ifstream में अंतर बताइए।
| ofstream | ifstream |
|---|---|
| File में data लिखने के लिए उपयोग होता है। | File से data पढ़ने के लिए उपयोग होता है। |
| Output File Stream | Input File Stream |
| Writing operation के लिए उपयोगी है। | Reading operation के लिए उपयोगी है। |
Q10. File handling में open() और close() functions का क्या महत्व है?
Answer: open() function file को specified mode में खोलने के लिए उपयोग होता है, जबकि close() function file operation पूरा होने के बाद file को बंद करने के लिए उपयोग होता है।
Q11. ios::out और ios::app में अंतर बताइए।
| ios::out | ios::app |
|---|---|
| File को writing के लिए open करता है। | File के अंत में data जोड़ने के लिए open करता है। |
| Existing content प्रभावित हो सकता है, विशेषकर truncation के साथ। | Existing content को बनाए रखते हुए नया data end में जोड़ा जाता है। |
Long Answer Questions
Q12. C++ में text file में data लिखने का program लिखिए।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream file("data.txt");
file << "Computer Science";
file.close();
cout << "Data written successfully";
return 0;
}
Data written successfully
Q13. C++ में text file से data पढ़ने का program लिखिए।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file("data.txt");
string data;
getline(file, data);
cout << data;
file.close();
return 0;
}
Computer Science
Q14. Text file की सभी lines को पढ़ने का C++ program लिखिए।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file("data.txt");
string line;
while (getline(file, line))
{
cout << line << endl;
}
file.close();
return 0;
}
Computer Science
C++ Programming
File Handling
Q15. C++ में file में नया data append करने का program लिखिए।
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream file("data.txt", ios::app);
file << "New Record" << endl;
file.close();
cout << "Data appended successfully";
return 0;
}
Data appended successfully
Quick Revision
- Text File: Readable text form में data store करने वाली file
- ofstream: File में data write करना
- ifstream: File से data read करना
- fstream: Read और Write दोनों
- ios::in: Reading mode
- ios::out: Writing mode
- ios::app: End में data जोड़ना
- getline(): पूरी line पढ़ना
- is_open(): File open status check करना
- close(): File बंद करना
Practice Questions
- Text File Handling क्या है?
- C++ में file handling के लिए कौन-सी header file उपयोग होती है?
- ofstream, ifstream और fstream क्या हैं?
- Text file में data कैसे write किया जाता है?
- Text file से data कैसे read किया जाता है?
- getline() function का उपयोग समझाइए।
- ios::app mode क्या है?
- ios::in और ios::out में अंतर बताइए।
- is_open() function का क्या उपयोग है?
- File को close करना क्यों आवश्यक है?
- एक text file में student का name, age और marks store करने का program लिखिए।
- Text file से पूरी line पढ़ने का C++ program लिखिए।
- Text file की सभी lines को पढ़ने का program लिखिए।
- Existing text file में नया data append करने का program लिखिए।
- ofstream और ifstream के बीच अंतर उदाहरण सहित समझाइए।