473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

binary file

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
7 3380
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
3
by: John R. Delaney | last post by:
I am running in debugging mode after a clean C++ compilation under .NET 2003. In a BIG loop (controlled many levels up in the call stack), I open a file with fopen using the "a" option. Then I write...
5
by: Neo | last post by:
Hello: I am receiving a Binary File in a Request from a application. The stream which comes to me has the boundary (Something like "---------------------------39<WBR>­0C0F3E0099" without the...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
10
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app)...
16
by: Erwin Moller | last post by:
Why is a binary file executable? Is any binary file executable? Is only binary file executable? Are all executable files binary? What is the connection between the attribute of binary and that of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.