Re: binary file and vc
Ludo wrote:
fp = fopen("C:\\Documents and Settings\\Ludo\\Bureau\\ezdata\\sample\
\sample1.ezd","rb");
Okay, opening for reading and disbling any CR/LF conversion.
char FileNote[1024];
fread(FileNote, sizeof(char), 1024, fp);
Two things here:
1. 'char' has by definition the size 1. The size of an object is defined as
a multiple of a 'char'.
2. You have two problems of possibly mismatching code here. Instead of using
1024 once in the declaration of FileNote and once in the call to fread(),
you should rather use 'sizeof FileNote' here. Then, this call would read
fread( FileNote, sizeof FileNote, 1, fp);
fread(XLable, sizeof(char), 50, fp);
fread(YLable, sizeof(char), 50, fp);
fread(XUnit, sizeof(char), 20, fp);
fread(YUnit, sizeof(char), 20, fp);
fread(&CurveCount, sizeof(long), 1, fp);
fread(Name, sizeof(char), 100, fp);
fread(&Time, sizeof(time_t), 1, fp);
fread(&StepX, sizeof(float), 1, fp);
fread(&StepY, sizeof(float), 1, fp);
fread(&XStart, sizeof(float), 1, fp);
fread(&Number, sizeof(long), 1, fp);
Similarly here, you could always have used
fread( X, sizeof X, 1, fp);
However, here you have another problem: the sizes of long, time_t and float
are not strictly defined. In particular long is often 32 bits but also
often 64 bits on 64 bit systems (Note: win64 has a 32 bit long)! Also, the
format of floats and the endianess might differ, but you are aware of that
already. My guess though is that time_t changed from 32 bits to 64 bits in
recent versions of VC and that that is breaking your code. When used
symmetrically, this doesn't explain why you can't load the file you saved
with the same program though.
printf("FormatVersion=%s\n", FormatVersion);
printf("FileNote=%s\n", FileNote);
printf("XLable=%s\n", XLable);
printf("YLable=%s\n", YLable);
printf("XUnit=%s\n", XUnit);
printf("YUnit=%s\n", YUnit);
Beware: nothing in your code makes sure that the strings you read were
terminated! In fact all error checking is either missing or was omitted for
the example only (I hope the latter is the case).
long CurveCount;
printf("CurveCount=%l\n", CurveCount);
*meeep* Wrong. The correct marker is '%ld'.
time_t Time;
printf("Time=%d\n",Time);
Again, but here you don't even have a choice, because there is no correct
way to display a time_t in printf(). You need to convert it first, but then
again you might lose some bits in the conversion. Maybe strftime() (not
sure about the name) could help ther.
printf("StepX=%f\n",StepX);
printf("StepY=%f\n",StepY);
printf("XStart=%f\n",XStart);
Okay.
printf("Number=%l\n",Number);
Wrong again, see CurveCount.
Two last comments:
1. If you used C++, you would probably use IOStreams for output and those
have overloaded operators<<. That means that automatically the correct type
is chosen and (assuming a decent compiler) a warning is issued if an unsafe
conversion is performed.
2. If you used a text-oriented layout for the file it would make it easier
to transfer it between systems. The different internal sizes of long or
time_t or the varying representation for float wouldn't matter then. It
might take some time for conversion, but ultimately I would use a
structuring helper like XML in which you format your own data eventually.
Uli