Microsoft KB Archive/60336

= The Fscanf function does not read consecutive lines as expected =

Article ID: 60336

Article Last Modified on 9/29/2005

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft C Professional Development System 5.1

 Microsoft C Professional Development System 6.0

 Microsoft C Professional Development System 6.0a

 Microsoft C Professional Development System 6.0a</li></ul>

 Microsoft C/C++ Professional Development System 7.0</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 1.5 Professional Edition</li></ul>

 Microsoft C Professional Development System 5.1</li></ul>

 Microsoft C Professional Development System 6.0</li></ul>

 Microsoft C Professional Development System 6.0a</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 2.1</li></ul>

<ul> <li>Microsoft Visual C++ 4.0 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ 4.1 Subscription</li></ul>

<ul> <li>Microsoft Visual C++ 4.2 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 5.0 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Service Pack 5</li></ul>

<ul> <li>Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual C++ 4.2 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual C++ 5.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q60336

<div class="summary_section">

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 &quot;[^\n]&quot;. 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: <ul> <li> Update the code to use the following Fscanf function call: fscanf(stream, &quot;%[^\n]%*c&quot;, line) The &quot;%*c&quot; format specifier reads one character from the input stream but does not assign it to any of the parameters in the fscanf function call. </li> <li>Call the Fgetc function after the Fscanf function call to move the file pointer beyond the '\n' character.</li></ul>

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 &quot;\n&quot; character.

Code sample
FILE *stream; char line[80];

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

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

Additional query words: 8.0 9.0 10.0 11.0

Keywords: kbtshoot kbprb kbcode KB60336

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.