C++ File Handling
C++ file handling lets you read/write data from files using streams. It's essential for storing data permanently.
C++ में फाइल हैंडलिंग से हम files में data पढ़ और लिख सकते हैं। यह डेटा को स्थायी रूप से सेव करने के लिए जरूरी है।
Common C++ File Handling Classes
ofstream
- For writing to filesifstream
- For reading from filesfstream
- For reading/writing both
Example 1: Create & Write to File
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outFile("example.txt");
if(outFile) {
outFile << "Hello, C++ File Handling!";
outFile.close();
cout << "File written successfully.";
} else {
cout << "Error opening file.";
}
return 0;
}
Output:
File written successfully.
Demonstrates: Create & Write to File
ofstream से नई फाइल बनाएं और लिखें
Example 2: Read from File
#include <iostream>
#include <fstream>
using namespace std;
#include <string>
int main() {
ifstream inFile("example.txt");
string line;
if(inFile) {
while(getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
} else {
cout << "File not found.";
}
return 0;
}
Output:
Hello, C++ File Handling!
Demonstrates: Read from File
ifstream से फाइल पढ़ें
Example 3: Append to File
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outFile("example.txt", ios::app);
outFile << "\nAppended line.";
outFile.close();
cout << "Line appended.";
return 0;
}
Output:
Line appended.
Demonstrates: Append to File
ios::app मोड से फाइल में जोड़ें
Example 4: Check File Exists
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inFile("example.txt");
if(inFile.is_open()) {
cout << "File exists.";
} else {
cout << "File does not exist.";
}
return 0;
}
Output:
File exists.
Demonstrates: Check File Exists
फाइल के खुलने की जांच करें
Example 5: Read Char by Char
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inFile("example.txt");
char ch;
if(inFile) {
while(inFile.get(ch)) {
cout << ch;
}
inFile.close();
}
return 0;
}
Output:
Hello, C++ File Handling! Appended line.
Demonstrates: Read Char by Char
फाइल को character by character पढ़ना
Example 6: Write Multiple Lines
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outFile("multilines.txt");
outFile << "Line 1\n";
outFile << "Line 2\n";
outFile << "Line 3\n";
outFile.close();
cout << "Lines written.";
return 0;
}
Output:
Lines written.
Demonstrates: Write Multiple Lines
ofstream से कई लाइंस लिखें
Example 7: Read Multiple Lines
#include <iostream>
#include <fstream>
using namespace std;
#include <string>
int main() {
ifstream inFile("multilines.txt");
string line;
while(getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
return 0;
}
Output:
Line 1 Line 2 Line 3
Demonstrates: Read Multiple Lines
ifstream से कई लाइंस पढ़ें
Example 8: Using fstream (both)
#include <iostream>
#include <fstream>
using namespace std;
#include <string>
int main() {
fstream file("both.txt", ios::out);
file << "First Line in fstream";
file.close();
file.open("both.txt", ios::in);
string line;
getline(file, line);
cout << line;
file.close();
return 0;
}
Output:
First Line in fstream
Demonstrates: Using fstream (both)
fstream से पढ़ना और लिखना
Example 9: File Overwrite
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outFile("example.txt");
outFile << "This will overwrite previous content.";
outFile.close();
cout << "File overwritten.";
return 0;
}
Output:
File overwritten.
Demonstrates: File Overwrite
ofstream से फाइल ओवरराइट करना
Example 10: Delete File
#include <iostream>
#include <fstream>
using namespace std;
#include <cstdio>
int main() {
if(remove("example.txt") == 0) {
cout << "File deleted successfully.";
} else {
cout << "File deletion failed.";
}
return 0;
}
Output:
File deleted successfully.
Demonstrates: Delete File
remove() से फाइल हटाएं