I'm trying to read two data (different types) from binary file (First I had written these data in it). I have problem with output. it's really unexpected:
0.000000000000000000000000 and 10000.
Where's the problem in reading double data type?
(OS: win xp; compiler tc 3.0; similar result i get with Visual C++ 6.0- result is -96745678999908980978989789 and 10000)
the code is the following:
#include <stdio.h>
enum {SUCCESS, FAIL};
struct dataForWrite
{
double DNum;
int INum;
};
int ErrorMsg(char *str);
void DataWrite(FILE *fout, struct dataForWrite *sd);
void DataRead(FILE *fin, char *string);
int main( )
{
int reval = SUCCESS;
FILE *fptr;
char filename[] = "strnum1.mix";
struct dataForWrite x;
x.DNum = 123.45;
x.INum = 10000;
if ((fptr = fopen(filename, "wb+")) == NULL)
{
reval = ErrorMsg(filename);
}
else
{
DataWrite(fptr, &x);
rewind(fptr);
DataRead(fptr, filename);
fclose(fptr);
}
return reval;
}
int ErrorMsg(char *str)
{
printf("cannot open the string %s\n", str);
return FAIL;
}
void DataWrite(FILE *fout, struct dataForWrite *sd)
{
printf("Ready for writting\n");
printf("writting the following data: %f %d\n", (*sd).DNum, (*sd).INum);
fprintf(fout, "%f %d", (*sd).DNum, (*sd).INum);
}
void DataRead(FILE *fin, char *string)
{
double fnum;
int inum;
printf("ready for reading from the following file: %s\n", string);
fscanf(fin, "%f %d", &fnum, &inum);
printf("reading the following data: %f %d\n", fnum, inum);
}