Microsoft KB Archive/49823

= PRB: Inconsistent Use of fscanf %x.xf Formatting =

Article ID: 49823

Article Last Modified on 12/12/2003

-

APPLIES TO

 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</li></ul>

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

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

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

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

-

<div class="notice_section">

This article was previously published under Q49823

<div class="notice_section">

<div class="summary_section">

SUMMARY
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: <pre class="fixed_text">  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


 * 1) include<stdio.h>

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

Keywords: kbcrt kbprb KB49823

-

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

© Microsoft Corporation. All rights reserved.