KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q50116: Fscanf() and White-Space Characters

Article: Q50116
Product(s): See article
Version(s): 5.00 5.10  |  5.10
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | | mspl13_c
Last Modified: 30-NOV-1989

Problem:

I try to read one integer on a line from a data file and then advance
the file pointer to the next line using the following:

    fscanf("%d\n", &INT);

However, I am unable to do this. The file pointer seems to jump to the
next character in the data file, wherever it may be, instead of
stopping at the first position of the next line.

Response:

According to the "Microsoft C for the MS-DOS Operating System Run-time
Library Reference," Page 501:

   A white-space character (in the format string) causes scanf to
   read, but not store, all consecutive white-space characters in the
   input up to the next non-white-space character. One white-space
   character in the format matches any number (including 0) and
   combination of white-space characters in the input.

In other words, because you have white space in your format specifier,
"\n", after the %d, fscanf() eats up all white space AFTER the
integer, including newline until the first nonwhite-space character is
found.

The following are possible workarounds:

1. Use the following fscanf() statement:

      fscanf("%d[^\n]%*c}, &INT)

   This tells fscanf() to read an integer and then read UNTIL it finds
   the "\n". At that point, it is necessary to read in the \n. The
   "%*c" reads, but does not store, one character.

2. Use fgets() to read in the line and then use sscanf to get the
   value, as follows:

      fgets( line, MAX_LINE_SIZE, stream );
            sscanf( line, "%d", &INT);

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.