I’m a complete noob to C and I wondered why if I take a user input why it wont find the file but when I hard code it using:
const char * fn = "/Users/james/Documents/test.rtf";
It seems to work fine?
char text[100];
fputs("File location: ", stdout);
fflush(stdout);
fgets(text, sizeof text, stdin);
FILE *fp = fopen(text,"r");
if( fp ) {
printf("\nFile Exists");
fclose(fp);
} else {
printf("\nFiles doesn't exist");
}
Any help would be awesome, or just a point to some online source that I have clearly not been able to find.
🙂
fgetsreads a line and keeps the final newline character. You’ll have to strip that off by(After doing the proper error checking, of course.)