| re: Stupid C Question: fscanf to read a whole line from file
Well to start with you are using fscanf incorrectly the prototype for fscanf is #
int fscanf( FILE *stream, const char *format [, argument ]... );
You have not put in the stream parameter. If you include the correct header files (stdio.h in this case), which you should always do then you would certainly have got a warning from your compiler if not an error.
Additionally even if you fixed that you are running a risk using fscanf like that because if the file contains a line longer than the sizeof(string) you will overwrite the end of the array and invoke undefined behaviour (which means anything could happen including you computer growing legging and going off to setup a hippie commune).
I suggest that you use fgets, prototype
char *fgets( char *string, int n, FILE *stream );
This function reads a line from a file but this the provisor that it doesn't read more than n bytes protecting against the case of overwriting the end of string.
|