I am using IAR embedded workbench for programming. I have eval board which supports microSD memory card. I have all the libraries and the application is working. I tried to access a file which does not exists in sd card to simulate failure and capture any file open error.
#include <errno.h>
#include <string.h>
int IsFileExists(const char* filename) {
errno = 0;
FILE* fptr = fopen(filename, "r");
printf("Error:%d %s",errno,strerror(errno))
if (fptr != NULL) {
fclose(fptr);
return 1;
}
return 0;
}
The fptr is NULL and errno is 0 always. Why the errno is not set to non-zero value?
Thanks!
You must declare errorno as extern value. and you have no need to set errorno as 0. It automatically sets after run fopen function. Refer: https://www.tutorialspoint.com/cprogramming/c_error_handling.htm