What will be the output of the following C program code if the file "test.txt" is empty,
What will be the output of the following C program code if the file "test.txt" is empty,
readable, and exists in the appropriate directory?
#include <stdio.h>
int main() {
FILE *fp = fopen("test.txt", "r");
if (fp == NULL) {
printf("Error\n");
} else {
printf("Success\n");
fclose(fp);
}
return 0;
}
Show answerHide answer
Correct answer: (A) Success
Explanation
fopen in "r" mode succeeds for an existing readable file even if it is empty, so fp is not NULL and "Success" is printed. Emptiness only matters when reading data.