C Files में Error Handling
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Errors Detect करने के लिए ferror() इस्तेमाल करना
ferror() सिर्फ यह report करता है कि पिछले operation पर कुछ गलत हुआ; यह नहीं बताता कि specifically क्या fail हुआ, इसलिए इसे आमतौर पर ज़्यादा detail के लिए perror() या errno check करने के साथ combine किया जाता है।
उदाहरण: Using ferror() to detect errors
// 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;
}
Login to try C/C++/Java/PHP code in the editor
feof() से EOF Check करना
feof() सिर्फ तब true होता है जब एक attempted read data खत्म होने की वजह से fail हो चुकी हो -- उस read attempt से पहले इसे check करना गलत तरीके से report करेगा कि आप अंत तक नहीं पहुंचे, आखिरी byte पर भी।
उदाहरण: Checking for EOF with feof()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
clearerr() से Error Indicators Clear करना
यह तब useful है जब आप original error cause करने वाली चीज़ fix करने के बाद दोबारा एक operation try करना चाहते हैं, क्योंकि error और EOF flags अन्यथा बने रहते हैं और उस stream पर आगे की operations block कर सकते हैं।
उदाहरण: Clearing error indicators with clearerr()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
fopenके बाद यह check न करना कि file pointerNULLहै।- loop condition की तरह
while (!feof(fp))इस्तेमाल करना, जो last line को दो बार process करता है। - एक error या EOF के बाद एक stream reuse करते समय
clearerrcall न करना।
- 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: