Reputation: 34523
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.
Upvotes: 0
Views: 301
Reputation: 1596
Use regex
char p[100];
char q[100];
char r[100];
fscanf(fp, "%[^\n]\n%[^\n]\n%[^\n]\nThe color of the car is %s",p,q,r, &var);
Modify it according to your requirement. I know this is not your actual string.
Upvotes: 1
Reputation: 8195
You have another bug here in that if test.txt doesn't exist, your code will go ahead and use fscanf anyway, making your check useless. This would almost certainly cause the program to crash.
Upvotes: 1
Reputation: 70382
You have a bug where you are passing in the pointer to an array of char into fscanf
, when you should be passing in a pointer to char. It so happens that the address of an array equals the address of the first element of an array, so it works on accident. But, the wrong type is being passed in.
In addition, you want to find the line which provides you with the match, and you seem to want to store "blue"
into var
. To do this, you should test to see if a line of input matches your scan pattern. You can use fgets
to read a line, and use sscanf
to scan the line.
while (fgets(buf, sizeof(buf), fp) != 0) {
result = sscanf(buf, "The color of the car is %s", var);
if (result == 1) break;
}
sscanf
will return how many directives it successfully scanned. In this case, there is the one %s
directive.
Doing it this way prevents a scanning error from "jamming" your input parsing. That is, if fscanf
returns an error, it will fail to move past the input that caused the error until you call clearerr()
.
Upvotes: 1
Reputation: 182619
How about using fgets
instead:
char *search = "The color of the..";
char *p;
while (fgets(var, SIZE, stdin)) {
/* Does it have what we want ? */
if ((p = strstr(var, search)))
break;
}
if (p)
p += strlen(str);
Upvotes: 4
Reputation:
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
result = fscanf(fp, "aaaaaaaaaaaaa\nbbbbbbbbbbb\ncccccccccc\nThe color of the car is %s", &var);
to achieve what you want.
Upvotes: 1