When fopen fails to open a non-existing file for reading, it sets errno to indicate the error, usually ENOENT meaning "No such file or directory".
Step 2: Use strerror to get error message
strerror(errno) converts the error code to a readable string, which will be "No such file or directory".
Final Answer:
Error: No such file or directory -> Option A
Quick Check:
fopen missing file error = "No such file or directory" [OK]
Hint: fopen missing file sets errno to ENOENT message [OK]
Common Mistakes:
Assuming errno is zero without checking fopen result
Confusing error messages for different errors
Not including string.h for strerror
4. Identify the error in this code snippet that uses errno:
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
errno = 0;
FILE *f = fopen("file.txt", "r");
if (f == NULL) {
perror("File open error");
}
if (errno != 0) {
printf("Error code: %d\n", errno);
}
return 0;
}
medium
A. Using perror instead of strerror is incorrect
B. Setting errno to 0 before fopen is unnecessary
C. Checking errno after fopen without checking fopen result first
D. Not closing the file pointer after fopen
Solution
Step 1: Analyze errno usage after fopen
errno should only be checked if the function indicates failure. Here, errno is checked regardless of fopen result, which can be misleading.
Step 2: Confirm correct error checking pattern
The correct pattern is to check if fopen returned NULL before reading errno. This code checks errno even if fopen succeeded.
Final Answer:
Checking errno after fopen without checking fopen result first -> Option C
Quick Check:
Check function result before errno [OK]
Hint: Always check function result before reading errno [OK]
Common Mistakes:
Reading errno without function failure
Confusing perror and strerror usage
Ignoring file close after fopen
5. You want to open a file for reading and print a custom error message if it fails. Which code snippet correctly uses errno and strerror to do this?
A) FILE *f = fopen("data.txt", "r");
if (!f) {
printf("Error: %s\n", strerror(errno));
}
B) errno = 0;
FILE *f = fopen("data.txt", "r");
if (f == NULL) {
printf("Error: %s\n", strerror(errno));
}
C) FILE *f = fopen("data.txt", "r");
if (f == NULL) {
errno = 0;
printf("Error: %s\n", strerror(errno));
}
D) FILE *f = fopen("data.txt", "r");
if (f == NULL) {
perror("Error opening file");
}
hard
A. Option A
B. Option B
C. Option C
D. Option D
Solution
Step 1: Check correct error handling with errno
Option D uses perror, which is the standard and simplest way to print a custom error message along with the error string corresponding to errno. It automatically handles errno correctly.
Step 2: Identify issues in other options
Option D does not reset errno before fopen, which is less safe. Option D resets errno before fopen and checks failure, but it's generally unnecessary to reset errno before calls that set it on failure. Option D resets errno after failure, clearing the error code before printing, which is incorrect.
Final Answer:
Option D -> Option D
Quick Check:
Use perror for simple and correct error messages [OK]
Hint: Use perror for automatic error message printing [OK]
Common Mistakes:
Resetting errno unnecessarily
Resetting errno after failure
Using strerror without valid errno
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.