The Fscanf function does not read consecutive lines as expected (60336)



The information in this article applies to:

  • The C Run-Time (CRT), when used with:
    • Microsoft C for MS-DOS 5.1
    • Microsoft C for MS-DOS 6.0
    • Microsoft C for MS-DOS 6.0a
    • Microsoft C for MS-DOS 6.0ax
    • Microsoft C/C++ for MS-DOS 7.0
    • Microsoft Visual C++ 1.0
    • Microsoft Visual C++ 1.5
    • Microsoft C for OS/2 5.1
    • Microsoft C for OS/2 6.0
    • Microsoft C for OS/2 6.0a
    • Microsoft Visual C++, 32-bit Editions 1.0
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 4.1
    • Microsoft Visual C++, 32-bit Editions 4.2
    • Microsoft Visual C++, 32-bit Editions 5.0
    • Microsoft Visual C++, 32-bit Editions 6.0
    • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
    • Microsoft Visual C++, 32-bit Professional Edition 4.2
    • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
    • Microsoft Visual C++, 32-bit Professional Edition 5.0
    • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
    • Microsoft Visual C++, 32-bit Professional Edition 6.0
    • Microsoft Visual C++, 32-bit Learning Edition 6.0

This article was previously published under Q60336

SUMMARY

When a file is open in text mode, an attempt to read lines of text by using the Fscanf function may fail and only one line of text is read from the file. The delimeter is set to "[^\n]". The Fscanf function reads up to but does not include the delimiting character. Therefore, the file stream stops at the first '\n' in the file. Subsequent Fscanf function calls fail because the file pointer remains at the delimiting character and the Fscanf function cannot advance the function pointer past it. To move the file pointer past the delimiting character, use one of the following two methods:
  • Update the code to use the following Fscanf function call:
          fscanf(stream, "%[^\n]%*c", line)
    The "%*c" format specifier reads one character from the input stream but does not assign it to any of the parameters in the fscanf function call.
  • Call the Fgetc function after the Fscanf function call to move the file pointer beyond the '\n' character.
The following code sample demonstrates this problem. The code sample should read and print lines from a text file until it reaches EOF. However, the code sample reads only the first line from the file. Since the end of file character has not been found, the code sample runs in an infinite loop if the file stream contains a "\n" character.

Code sample

FILE *stream;
char line[80];

while ((fscanf(stream, "%[^\n]", line)) != EOF )
{
   printf("Line = %s \n",line);
}
The following code example demonstrates the second method above to work around this problem:
FILE *stream;
char line[80];

while ((fscanf(stream, "%[^\n]", line)) != EOF)
{
   fgetc(stream);    // Reads in '\n' character and moves file
                     // stream past delimiting character
   printf("Line = %s \n", line);
}

Modification Type:MajorLast Reviewed:9/29/2005
Keywords:kbtshoot kbcode kbprb KB60336 kbAudDeveloper