Microsoft KB Archive/49823

From BetaArchive Wiki
Knowledge Base

PRB: Inconsistent Use of fscanf %x.xf Formatting

Article ID: 49823

Article Last Modified on 12/12/2003


  • The C Run-Time (CRT), when used with:
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 1.5 Professional Edition
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 2.0 Professional Edition
    • Microsoft Visual C++ 2.1
    • Microsoft Visual C++ 4.0 Standard Edition
    • Microsoft Visual C++ 5.0 Standard Edition
    • Microsoft Visual C++ 6.0 Service Pack 5

This article was previously published under Q49823


Using the Microsoft C run-time library function fscanf() with the format specifier %x.xf to read floating-point values usually produces undesirable results. This happens especially if the file contains floating point information that was not recorded using the same %x.x specifier that is being used to read.

The sample code below can be used as an example of expected behavior. The first output line shows the input values, the second shows what an incorrect fscanf() format specifier reads, and the third is the correct data read by fscanf(). The output from the program is as follows:

   Values: 3.104000, 34.235340, 834.343201, 5968.394043
   After Read: 3.100000, 419561472.000000, 0.000003, 4.227506
   After Rewind: 3.104000, 34.235340, 834.343201, 5968.394043

The output of the second line (After Read) may differ between compilers and may depend upon the compiler options, but is still not the expected values. This is not a problem with the fscanf() function; it is due to using a different floating point specifier to read the value than was used to write the value.

In the sample code below, the first value is read correctly. After the first value is read, the file pointer points to the second 0 (zero) in 3.104000. When the second value is read, the information in the file does not conform to floating-point format, and zeros are read after that. The compiler has no way of knowing how far to move the file pointer to get to the next value. Read floating point values with the same specifier that was used to write them. Once a value has been read correctly, it may be truncated or formatted as desired. For more information on the scanf() family of functions, please refer to the Visual C++ Books Online.

The following sample code demonstrates the importance of reading floating point values with the same type specifier that was used in writing them. Refer to the SUMMARY above for the expected program output.

Sample Code

/* Compile options needed: none


void main( void )
   float a = 3.104f, b = 34.23534f, c = 834.3432f, d = 5968.394f;
   float ar, br, cr, dr;
   FILE *stream;

   if( (stream = fopen( "file.tst", "a" )) == NULL )
      printf( "ERROR:  Unable to open output file" );
      fprintf( stream, "%f,%f,%f,%f\n", a, b, c, d );
   fclose( stream );
   if( (stream = fopen( "file.tst", "r" )) == NULL )
      printf( "ERROR:  Unable to open input file" );
                    /* NOTE:  %x.x format specifier */ 
   {                /*  |  */ 
      printf("Values: %f, %f, %f, %f\n", a, b, c, d );
      fscanf( stream, "%4f,%4f,%4f,%4f", &ar, &br, &cr, &dr );
      printf("After Read: %f, %f, %f, %f\n", ar, br, cr, dr );
      fscanf( stream, "%f,%f,%f,%f", &ar, &br, &cr, &dr );
      printf("After Rewind: %f, %f, %f, %f\n", ar, br, cr, dr );

Keywords: kbcrt kbprb KB49823