D
David Mathog
Apologies if this is in the FAQ. I looked, but didn't find it.
In a particular program the input read from a file is supposed to be:
+ 100 200 name1
- 101 201 name2
It is parsed by reading the + character, and then sending the
remainder into fscanf() like
count = fscanf(fp,"%d %d %s",&first_int,&second_int,&string);
This works fine unless the input is bogus. In particular, if
"name1" is left off, fscanf happily reads past the EOL of the
first line and comes back with "-" from the second line
stored in the string. Effectively it sees the bogus line as:
+ 100 200 - 101 201 name2
since it makes no distinction between EOL and other white space.
So count is 3 but the wrong characters are stored in string.
What I want is for count to be 2 and string's contents to be
undefined. Is there some magic format specifier that tells fscanf()
not to go past the EOL when looking for data? Sure, it can be done by
reading a whole line into a buffer, and then using sscanf() on that. It
just seems that there should be a way to make fscanf() "line aware".
Possible?
Thanks,
David Mathog
In a particular program the input read from a file is supposed to be:
+ 100 200 name1
- 101 201 name2
It is parsed by reading the + character, and then sending the
remainder into fscanf() like
count = fscanf(fp,"%d %d %s",&first_int,&second_int,&string);
This works fine unless the input is bogus. In particular, if
"name1" is left off, fscanf happily reads past the EOL of the
first line and comes back with "-" from the second line
stored in the string. Effectively it sees the bogus line as:
+ 100 200 - 101 201 name2
since it makes no distinction between EOL and other white space.
So count is 3 but the wrong characters are stored in string.
What I want is for count to be 2 and string's contents to be
undefined. Is there some magic format specifier that tells fscanf()
not to go past the EOL when looking for data? Sure, it can be done by
reading a whole line into a buffer, and then using sscanf() on that. It
just seems that there should be a way to make fscanf() "line aware".
Possible?
Thanks,
David Mathog