I'm trying to read a line using the following code:
while(fscanf(f, "%[^\n\r]s", cLine) != EOF )
{
/* do something with cLine */
}
But somehow I get only the first line every time. Is this a bad way to read a line? What should I fix to make it work as expected?
If you try
while( fscanf( f, "%27[^\n\r]", cLine ) == 1 )
you might have a little more luck. The three changes from your original:27
here as an example, and unfortunately thescanf()
family require the field width literally in the format string and can't use the*
mechanism that theprintf()
can for passing the value ins
in the format string -%[
is the format specifier for "all characters matching or not matching a set", and the set is terminated by a]
on its ownThat said, you'll get the same result with less pain by using
fgets()
to read in as much of a line as will fit in your buffer.