C++ File Handling का परिचय
In this page:
#include <fstream>
std::ofstream file_name("filename.txt"); // open for writing
std::ifstream file_name("filename.txt"); // open for reading
file_name.close();
Streams और Files
C++ fstream library के जरिए file I/O handle करता है, जो cin और cout जैसे ही stream abstraction पर build होती है।
Keyboard से पढ़ने या console पर लिखने की बजाय, file streams disk पर एक file से पढ़ते और लिखते हैं, almost identical syntax इस्तेमाल करते हुए जो आप पहले से console I/O से जानते हैं।
उदाहरण: Streams and Files
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::ofstream file("data.txt");
file << "Hello, file!";
file.close();
// Print to the console with cout
std::cout << "Written using the same << stream style as cout" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक File Open करना
आप एक existing stream object पर open() member function call करके, या stream के constructor में सीधे file path pass करके एक file open कर सकते हैं -- constructor form file को तुरंत open करता है, एक separate call बचाते हुए।
उदाहरण: Opening a File
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::ofstream file1;
file1.open("data.txt");
std::ofstream file2("data.txt");
file1 << "Line 1";
file1.close();
file2.close();
// Print to the console with cout
std::cout << "Opened both ways" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Check करना कि File Open Succeed हुआ
एक file कई वजहों से open होने में fail हो सकती है -- path में एक typo, missing permissions, या एक nonexistent directory -- इसलिए पढ़ने या लिखने की कोशिश करने से पहले हमेशा is_open() call करें (या stream को एक boolean context में check करें)।
यह check skip करना silent failures की ओर ले जाता है जहाँ हर बाद का operation बस कुछ नहीं करता।
उदाहरण: Checking if File Open Succeeded
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::ofstream file("data.txt");
// Check whether file.is_open()
if (file.is_open()) {
file << "Success";
file.close();
}
// Print to the console with cout
std::cout << "Checked before writing" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक File Close करना
C++ automatically एक open file stream close कर देता है जब इसका destructor चलता है, typically scope के अंत में, जो ज़्यादातर resource leaks रोकता है भले ही आप भूल जाएँ।
फिर भी, काम खत्म होने पर explicitly close() call करना अच्छी practice है -- यह scope exit का wait करने की बजाय buffered writes तुरंत disk पर flush करता है।
उदाहरण: Closing a File
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::ofstream file("data.txt");
file << "Content";
file.close();
// Print to the console with cout
std::cout << "Closed explicitly" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
File Open Modes
ios::out (write, existing content overwrite करते हुए) और ios::app (existing को erase किए बिना अंत में append करना) जैसे open mode flags control करते हैं कि existing file content का क्या होता है।
गलत mode चुनना एक common bug है -- append करने का मतलब होने पर accidentally ios::out इस्तेमाल करना silently एक file के contents मिटा देगा।
उदाहरण: File Open Modes
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::ofstream file("data.txt", std::ios::app);
file << "Appended line\n";
file.close();
// Print to the console with cout
std::cout << "Appended using ios::app" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- पढ़ने या लिखने से पहले यह check न करना कि file successfully open हुई या नहीं।
- file close करना भूल जाना, इसलिए buffered data शायद न लिखा जाए।
- गलत relative path इस्तेमाल करना ताकि file न मिले।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: