C++ में File Error Handling
In this page:
std::ifstream file_name("filename.txt");
if (file_name.fail()) {
// could not open or read
}
file_name.clear(); // reset error flags
Stream State Flags
हर stream internal state flags maintain करता है जो record करते हैं कि last operation के दौरान क्या हुआ: eof() report करता है कि stream file के अंत तक पहुँच गया या नहीं, जबकि fail() एक ज़्यादा general failure report करता है, जैसे एक number expect करना लेकिन text मिलना।
उदाहरण: Stream State Flags
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::ifstream file("nonexistent.txt");
// Print to the console with cout
std::cout << "eof=" << file.eof() << " fail=" << file.fail() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Stream Flags Clear करना
एक बार कोई error flag set हो जाए, एक stream आगे operations perform करने से मना कर देता है, भले ही underlying problem temporary हो या पहले से resolved -- clear() call करना उन flags को वापस एक working state में reset करता है ताकि आप stream इस्तेमाल करना जारी रख सकें (उदाहरण के लिए, input की एक bad line handle करने के बाद)।
उदाहरण: Clearing Stream Flags
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::ifstream file("nonexistent.txt");
// Check whether file.fail()
if (file.fail()) {
// Print to the console with cout
std::cout << "Error detected" << std::endl;
file.clear();
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Missing Files Handle करना
एक ऐसी file open करने की कोशिश जो exist न करे default से कोई exception throw नहीं करती -- यह बस stream को एक failed state में छोड़ देती है।
Open करने के तुरंत बाद is_open() या stream की boolean state check करना इसे catch कर लेता है इससे पहले कि आप एक stream से uninitialized या garbage data पढ़ने की कोशिश करें जो actually कभी एक file से connected ही नहीं हुई थी।
उदाहरण: Handling Missing Files
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::ifstream file("nonexistent.txt");
// Check whether !file.is_open()
if (!file.is_open()) {
// Print to the console with cout
std::cout << "File does not exist" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Writing Errors
एक write उन वजहों से fail हो सकता है जिनका आपके code से कोई लेना-देना नहीं, जैसे एक full disk या एक read-only filesystem।
एक write के बाद good() check करना confirm करता है कि कोई भी error flags (eof, fail, या bad) set नहीं हैं, आपको confidence देते हुए कि data actually written हुआ, silently drop नहीं हुआ।
उदाहरण: Writing Errors
// 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 << "Some content";
// Check whether file.good()
if (file.good()) {
// Print to the console with cout
std::cout << "Write confirmed successful" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Files के साथ Exception Handling
उचित flags के साथ stream.exceptions() call करना एक stream को configure करता है ताकि जब कोई error हो तो यह बस चुपचाप एक flag set करने की बजाय एक std::ios_base::failure exception throw करे -- यह आपको program में दूसरी exceptional conditions के लिए इस्तेमाल की जाने वाली same try/catch structure से file errors handle करने देता है।
उदाहरण: Exception Handling with Files
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::ifstream file;
file.exceptions(std::ios::failbit);
// Try code that might throw an exception
try {
file.open("nonexistent.txt");
// Handle the exception if one was thrown
} catch (std::ios_base::failure &e) {
// Print to the console with cout
std::cout << "Exception thrown for file error" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- पढ़ने के बाद stream state flags check न करना।
- एक error के बाद stream reuse करने से पहले
clear()call करना भूल जाना। - बिना
is_open()check किए यह मान लेना कि file exist करती है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: