← Back to C Course | Chapter 10: File I/O | Lesson 6 of 6

C Files में Error Handling

File error handling का मतलब है यह check करना कि खोलना, पढ़ना, या लिखना काम किया या नहीं, अंदर जाने से पहले यह पक्का करने जैसा कि एक दरवाज़ा खुला। C आपको detect और describe करने के tools देता है कि क्या गलत हुआ।
Syntax
c
FILE *file_pointer = fopen("filename.txt", "mode");

if (file_pointer == NULL) {
  // handle the error
}

if (ferror(file_pointer)) {
  // a read or write failed
}

NULL File Pointers Handle करना

यह check हर fopen() call के तुरंत बाद होना चाहिए, किसी भी दूसरे file operation की कोशिश करने से पहले, क्योंकि बाद का हर function मान लेता है कि यह एक valid, open file stream के साथ काम कर रहा है।

उदाहरण: Handling NULL file pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("nonexistent.txt", "r");
	// Check whether fp == NULL
	if (fp == NULL) {
		// Print formatted text to the screen
		printf("Failed to open file");
		// Send 1 back to the caller
		return 1;
	}
	fclose(fp);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Errors Detect करने के लिए ferror() इस्तेमाल करना

ferror() सिर्फ यह report करता है कि पिछले operation पर कुछ गलत हुआ; यह नहीं बताता कि specifically क्या fail हुआ, इसलिए इसे आमतौर पर ज़्यादा detail के लिए perror() या errno check करने के साथ combine किया जाता है।

उदाहरण: Using ferror() to detect errors

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("data.txt", "w");
	fprintf(fp, "test");
	// Check whether ferror(fp)
	if (ferror(fp)) {
		// Print formatted text to the screen
		printf("Error occurred");
	} else {
		// Print formatted text to the screen
		printf("No error");
	}
	fclose(fp);
	// Return 0 to signal that the program finished successfully
	return 0;
}

feof() से EOF Check करना

feof() सिर्फ तब true होता है जब एक attempted read data खत्म होने की वजह से fail हो चुकी हो -- उस read attempt से पहले इसे check करना गलत तरीके से report करेगा कि आप अंत तक नहीं पहुंचे, आखिरी byte पर भी।

उदाहरण: Checking for EOF with feof()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("data.txt", "w");
	fprintf(fp, "Hi");
	fclose(fp);
	fp = fopen("data.txt", "r");
	char buffer[10];
	while (fgets(buffer, 10, fp) != NULL) {}
	// Print formatted text to the screen
	printf("%d", feof(fp));
	fclose(fp);
	// Return 0 to signal that the program finished successfully
	return 0;
}

clearerr() से Error Indicators Clear करना

यह तब useful है जब आप original error cause करने वाली चीज़ fix करने के बाद दोबारा एक operation try करना चाहते हैं, क्योंकि error और EOF flags अन्यथा बने रहते हैं और उस stream पर आगे की operations block कर सकते हैं।

उदाहरण: Clearing error indicators with clearerr()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("data.txt", "w");
	fprintf(fp, "test");
	clearerr(fp);
	// Print formatted text to the screen
	printf("%d", ferror(fp));
	fclose(fp);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Detailed File Errors के लिए perror() इस्तेमाल करना

एक generic error message के उलट, perror() automatically failure के लिए system की actual explanation add करता है (जैसे 'No such file or directory' या 'Permission denied'), जो अकेले एक bare NULL check से debugging के लिए कहीं ज़्यादा useful है।

उदाहरण: Using perror() for detailed file errors

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("nonexistent.txt", "r");
	// Check whether fp == NULL
	if (fp == NULL) {
		perror("File open failed");
	}
	// 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. fopen के बाद यह check न करना कि file pointer NULL है।
  2. loop condition की तरह while (!feof(fp)) इस्तेमाल करना, जो last line को दो बार process करता है।
  3. एक error या EOF के बाद एक stream reuse करते समय clearerr call न करना।
चैप्टर सारांश
  • File handling fopen() से एक file खोलने और fclose() से इसे बंद करने से शुरू होती है।
  • fread(), fwrite(), fprintf(), और fscanf() binary या formatted data पढ़ते और लिखते हैं।
  • fseek() और ftell() एक file में move करते हैं, और files के साथ काम करते समय आपको errors check करने चाहिए।
🔒

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.