binary file

L

Ludo

Hi,
I'm working on an open source project and I have a problem with a
binary file. When I read the binary file with the program downloaded
from sourceforge, I have no problem. However, I'm unable to read this
binary file with the program compiled with Microsoft Visual C++
Express Edition SP1. The program read wrong values from the file and
crash. I don't understand because:
*the program downloaded from sourceforge was compiled with Microsoft
Visual C++ (in 2004)
*it is not a problem of big/little endian.
*C I/O functions and C++ I/O class read the same wrong values from
the files.

Any idea?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi,
I'm working on an open source project and I have a problem with a
binary file. When I read the binary file with the program downloaded
from sourceforge, I have no problem. However, I'm unable to read this
binary file with the program compiled with Microsoft Visual C++
Express Edition SP1. The program read wrong values from the file and
crash. I don't understand because:
*the program downloaded from sourceforge was compiled with Microsoft
Visual C++ (in 2004)
*it is not a problem of big/little endian.
*C I/O functions and C++ I/O class read the same wrong values from
the files.

Without seeing your code, we can't say anything for sure. However, there
is nothing in the C++ language or your compiler that prevents you from
reading binary files. I can see two possible reasons for this problem,
the first is that the other program you used performs some kind of
modification to the data, or the way you try to read the data is wrong.
Did you specify that the file should be read as a binary file when
opening it (ios_base::binary)?
 
L

Ludo

Without seeing your code, we can't say anything for sure.

This is the function that read the data from the file :

bool ezDataCurve::LoadFromFile(FILE *fp)
{
fread(mName, sizeof(char), MAX_CURVE_NAME_LENGTH, fp);
fread(&mTime, sizeof(time_t), 1, fp);
fread(&mStepX, sizeof(float), 1, fp);
fread(&mStepY, sizeof(float), 1, fp);
fread(&mXStart, sizeof(float), 1, fp);
fread(&mNumber, sizeof(long), 1, fp);

mpData = new long[mNumber];
if( mpData == NULL )
{ mNumber = 0;
return false;
}
mNumber = fread(mpData, sizeof(long), mNumber, fp);

ResetDataRange();
return true;
}

And this one for writing data to the file:

bool ezDataCurve::SaveToFile(FILE *fp)
{ fwrite(mName, sizeof(char), MAX_CURVE_NAME_LENGTH, fp);
fwrite(&mTime, sizeof(time_t), 1, fp);
fwrite(&mStepX, sizeof(float), 1, fp);
fwrite(&mStepY, sizeof(float), 1, fp);
fwrite(&mXStart, sizeof(float), 1, fp);
fwrite(&mNumber, sizeof(long), 1, fp);
fwrite(mpData, sizeof(long), mNumber, fp);

return true;
}
However, there
is nothing in the C++ language or your compiler that prevents you from
reading binary files. I can see two possible reasons for this problem,
the first is that the other program you used performs some kind of
modification to the data, or the way you try to read the data is wrong.

I have compiled the program with the source files downloaded from
sourceforge without modification.
Did you specify that the file should be read as a binary file when
opening it (ios_base::binary)?

Yes it is : FILE *fp = fopen(mFileName, "rb");
 
D

dolphin

Hi,
I'm working on an open source project and I have a problem with a
binary file. When I read the binary file with the program downloaded
from sourceforge, I have no problem. However, I'm unable to read this
binary file with the program compiled with Microsoft Visual C++
Express Edition SP1. The program read wrong values from the file and
crash. I don't understand because:
*the program downloaded from sourceforge was compiled with Microsoft
Visual C++ (in 2004)
*it is not a problem of big/little endian.
*C I/O functions and C++ I/O class read the same wrong values from
the files.

Any idea?

I ever downloaded some source codes from sourceforge. I think it is
the problem about the edition of your VC.
Sometimes I downloaded sources written in VS2003, and I can not
compile it in VS2005.
 
L

Ludo

I ever downloaded some source codes from sourceforge. I think it is
the problem about the edition of your VC.
Sometimes I downloaded sources written in VS2003, and I can not
compile it in VS2005.

I think so. I have written a C program that reads data from the file.
The problem seems to be only with numeric variables and more precisely
with long type. The other variables are float, char and time_t type
and the values seem to be ok.
 
L

Ludo

I have solved the problem. The problem is due to time_t :

« In Visual C++ 2005, time is a wrapper for _time64 and time_t is, by
default, equivalent to __time64_t. If you need to force the compiler
to interpret time_t as the old 32-bit time_t, you can define
_USE_32BIT_TIME_T. This is not recommended because your application
may fail after January 18, 2038; the use of this macro is not allowed
on 64-bit platforms. »

Thanks, Ludo
 
O

Ondra Holub

Without seeing your code, we can't say anything for sure.

This is the function that read the data from the file :

bool ezDataCurve::LoadFromFile(FILE *fp)
{
fread(mName, sizeof(char), MAX_CURVE_NAME_LENGTH, fp);
fread(&mTime, sizeof(time_t), 1, fp);
fread(&mStepX, sizeof(float), 1, fp);
fread(&mStepY, sizeof(float), 1, fp);
fread(&mXStart, sizeof(float), 1, fp);
fread(&mNumber, sizeof(long), 1, fp);

mpData = new long[mNumber];
if( mpData == NULL )
{ mNumber = 0;
return false;
}
mNumber = fread(mpData, sizeof(long), mNumber, fp);

ResetDataRange();
return true;

}

And this one for writing data to the file:

bool ezDataCurve::SaveToFile(FILE *fp)
{ fwrite(mName, sizeof(char), MAX_CURVE_NAME_LENGTH, fp);
fwrite(&mTime, sizeof(time_t), 1, fp);
fwrite(&mStepX, sizeof(float), 1, fp);
fwrite(&mStepY, sizeof(float), 1, fp);
fwrite(&mXStart, sizeof(float), 1, fp);
fwrite(&mNumber, sizeof(long), 1, fp);
fwrite(mpData, sizeof(long), mNumber, fp);

return true;

}
However, there
is nothing in the C++ language or your compiler that prevents you from
reading binary files. I can see two possible reasons for this problem,
the first is that the other program you used performs some kind of
modification to the data, or the way you try to read the data is wrong.

I have compiled the program with the source files downloaded from
sourceforge without modification.
Did you specify that the file should be read as a binary file when
opening it (ios_base::binary)?

Yes it is : FILE *fp = fopen(mFileName, "rb");

Hi. I suggest to change
fwrite(&mTime, sizeof(time_t), 1, fp);
to
fwrite(&mTime, sizeof(mTime), 1, fp);

But I do not think this is the problem, it is only more safe.
 
J

Jorgen Grahn

This is the function that read the data from the file :

bool ezDataCurve::LoadFromFile(FILE *fp)
{
fread(mName, sizeof(char), MAX_CURVE_NAME_LENGTH, fp);
fread(&mTime, sizeof(time_t), 1, fp);
fread(&mStepX, sizeof(float), 1, fp);
fread(&mStepY, sizeof(float), 1, fp);
fread(&mXStart, sizeof(float), 1, fp);
fread(&mNumber, sizeof(long), 1, fp);
....

As a side note, this is a stupid design for a binary file format (it's
not your code, so I can be blunt, right?).

All those sizes (except char) can and will change between operating
systems, releases and CPU architectures. Not to mention the
representation within the values, e.g. big- and little-endian.

(Even if you live on Windows only, I think you will have problems when
the writer runs on x86 and the reader on AMD64 -- long may be a 64-bit
type on the latter.)

/Jorgen
 
L

Ludo

As a side note, this is a stupid design for a binary file format (it's
not your code, so I can be blunt, right?).

It's not my code, so you can.
All those sizes (except char) can and will change between operating
systems, releases and CPU architectures. Not to mention the
representation within the values, e.g. big- and little-endian.

(Even if you live on Windows only, I think you will have problems when
the writer runs on x86 and the reader on AMD64 -- long may be a 64-bit
type on the latter.)

I have send an mail to the main developer and I have suggested to get
rid of this binary file format, which is the native format, and
replace it by a text file based on an xml format in order to avoid the
problems you mentioned about portatibility.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

It's not my code, so you can.


I have send an mail to the main developer and I have suggested to get
rid of this binary file format, which is the native format, and
replace it by a text file based on an xml format in order to avoid the
problems you mentioned about portatibility.

Just going to a text-based format would help, and avoid the added
complexity of coming up with an XML schema and parsing the XML-file.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top