test.txt:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
ccccccccccccccccccccccccccccccccccc
The color of the car is blue
code:
FILE *fp;
char var[30];
int result
if( (fp = fopen( "test.txt", "r")) == NULL) //open for read
printf("ERROR");
result = fscanf( fp, "The color of the car is %s", &var);
After this executes:
opens file (not NULL and was able to execute an append when testing)
result = 0 //zero- in the case of a matching failure….?
errno = 0;
and var is garbage.
I was expecting fscanf() to match “blue”.
How should I correctly get blue into var?
Thank You.
Fscanf doesn’t work like this. It doesn’t look around nor scan the string for stuff to match. You have to supply a string exactly matching the format specifier. So you could do stuff like
to achieve what you want.