File Open Error Handler
C
Easy
2 views
Problem Description
Open the file. If null is returned, check errno and print the specific error message (file not found, permission denied, etc.). perror() does that.
Official Solution
#include <stdio.h>
#include <errno.h>
int main() {
FILE *fp;
fp = fopen("data.txt", "r");
if (fp == NULL) {
// perror prints message based on errno
perror("Error opening file");
return 1;
}
printf("File opened successfully.n");
fclose(fp);
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!