← Back to C++ Course | Chapter 15: File I/O | Lesson 5 of 6

C++ में File Error Handling

File error handling यह check करना है कि एक file open, read, या write करना काम किया या नहीं, अंदर जाने से पहले यह सुनिश्चित करने की तरह कि एक door खुला। Streams में flags होते हैं जो बताते हैं कि क्या गलत हुआ।
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. पढ़ने के बाद stream state flags check न करना।
  2. एक error के बाद stream reuse करने से पहले clear() call करना भूल जाना।
  3. बिना is_open() check किए यह मान लेना कि file exist करती है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.