Wednesday, December 12, 2007

ANSI C way to check if a file is readable

I needed a dead simple ANSI C compatible way to check if a file was readable. So here it is:


int can_read_file(const char * filename) {
FILE *file=NULL;
if ((file = fopen(filename, "r"))) {
fclose(file);
return 1;
}
return 0;
}


You'll have to #include stdio.h

No comments: