Floating point fread bug with C++ .NET 2003 [code attached]

P

Phil Scadden

Couldnt submit this to MS without paying $$$ but here is a bug with C++.

This tiny snippet reads from a file containing a single DOUBLE

#include<stdio.h>

int main()
{
double output;
FILE *test_out;

test_out = fopen("test.out","r");
fread (&(output),sizeof(double),1,test_out);
fclose(test_out);
printf("Output: %e\n", output);
}

Problem is that output is wrong - should be 4890.695 but get rubbish
instead. It is a different no. to what
was written with fwrite. The bizarre thing is that for most numbers it works
fine but this one (and others),
I get rubbish. If I compile exactly the same code with other C compilers on
the same machine, they read the
file correctly. I infer this is BUG in MS C++.

Attached is file with the code and the 8 byte input file if anyone wants to
confirm this.
 
V

Victor Bazarov

Phil said:
Couldnt submit this to MS without paying $$$ but here is a bug with
C++.

This tiny snippet reads from a file containing a single DOUBLE

#include<stdio.h>

int main()
{
double output;
FILE *test_out;

test_out = fopen("test.out","r");

Try

test_out = fopen("test.out", "rb");
fread (&(output),sizeof(double),1,test_out);
fclose(test_out);
printf("Output: %e\n", output);
}

Problem is that output is wrong [...]

RTFM. If you open the file to be read unformatted, open it as binary.

V
 
C

Carl Daniel [VC++ MVP]

Phil said:
Couldnt submit this to MS without paying $$$ but here is a bug with
C++.

In addition to Victor's completely correct response, and for the benefit of
other readers, you can submit bug reports for visual studio at the MSDN
Product Feedback Center:

http://lab.msdn.microsoft.com/productfeedback/

....although you probably should research your "bug" a bit more carefully
before concluding it's a bug and reporting it.

-cd
 
P

Phil Scadden

Thank you Victor. Changing the open statement does work. In my defense, I
will say that is code is being ported onto Windows, and in its original
platform, the manual say "b" mean binary but is unnecessary. Oddly, it works
for 99% of floating point values - well to be precise it got the number
wrong twice in run over 89000 values. This bug report came down from
detailed investigation of those two strange value that did not match other
original from other systems nor from gcc on windows.

Thank you very much for that link. I did not find this from the MS support
site. In fact the MS New Zealand site does not have a product support link
for C++ .NET 2003, only 2002 or VS 2003 (which does not
accept the C++ standard product id). Phone support did not point to this
link either.

However, the error is mine in the code and I am extremely appreciative of
the help that given. My response on Microsoft support was surly for which I
apologise but offer the above frustrating experience in mitigation.

Thanks again, one and all.
 
C

Carl Daniel [VC++ MVP]

Phil said:
Thank you Victor. Changing the open statement does work. In my
defense, I will say that is code is being ported onto Windows, and in
its original platform, the manual say "b" mean binary but is
unnecessary. Oddly, it works for 99% of floating point values - well
to be precise it got the number wrong twice in run over 89000 values.
This bug report came down from detailed investigation of those two
strange value that did not match other original from other systems
nor from gcc on windows.

Actually, it'll have problems for any values that have the byte 0x0d in
their binary representation. - overall, something on the order of 1 in 50
double values should have problems.
Thank you very much for that link. I did not find this from the MS
support site. In fact the MS New Zealand site does not have a product
support link for C++ .NET 2003, only 2002 or VS 2003 (which does not
accept the C++ standard product id). Phone support did not point to
this link either.

VS 2003 is C++ .NET 2003 (or rather, a superset of it).
However, the error is mine in the code and I am extremely
appreciative of the help that given. My response on Microsoft support
was surly for which I apologise but offer the above frustrating
experience in mitigation.

Thanks again, one and all.

-cd
 
P

Phil Scadden

VS 2003 is C++ .NET 2003 (or rather, a superset of it).

It would not accept the C++ .NET 2003 product id however (it was clearly not
Enterprise nor architect which were the two options offered.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top