By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,226 Members | 1,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,226 IT Pros & Developers. It's quick & easy.

binary file

P: n/a
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?

Aug 23 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 2007-08-23 13:06, Ludo wrote:
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)?

--
Erik Wikström
Aug 23 '07 #2

P: n/a
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");

Aug 23 '07 #3

P: n/a
On Aug 23, 7:06 pm, Ludo <lmout1...@yahoo.frwrote:
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.

Aug 24 '07 #4

P: n/a
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.

Aug 24 '07 #5

P: n/a
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

Aug 24 '07 #6

P: n/a
On 23 Srp, 14:09, Ludo <lmout1...@yahoo.frwrote:
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.

Aug 24 '07 #7

P: n/a
On Thu, 23 Aug 2007 05:09:36 -0700, Ludo <lm*******@yahoo.frwrote:
>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);
....

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

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Aug 24 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.