473,769 Members | 5,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3428
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::bina ry)?

--
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::Lo adFromFile(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::Sa veToFile(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::bina ry)?
Yes it is : FILE *fp = fopen(mFileName , "rb");

Aug 23 '07 #3
On Aug 23, 7:06 pm, Ludo <lmout1...@yaho o.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...@yaho o.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::Lo adFromFile(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::Sa veToFile(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::bina ry)?

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*******@yaho o.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::Lo adFromFile(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.dyn dns.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
15257
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 pattern can occur on any byte boundary in the file, so chunking through the code at 16 bytes a frame maybe a problem. The file itself isn't so large, maybe 32 kbytes is all and the need for speed is not so great, but the need for accuracy in the...
28
2802
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
9
6520
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 delta binary is pretty huge in size. I have 1 byte file and 2 bytes file, the delta should be 1 byte but somehow it turns out to be 249 bytes using binary formatter. I guess serialization has some other things added to the delta file.
3
2128
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 23 doubles to it with fwrite, one call for each double. Then I close the file using fclose. After three times around the loop in the debugger, I stop the program (using "Stop debugging"). That is writing 552 bytes. The resulting file's properties...
5
5314
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 quotes), and also some more text like this and file name (e.g. "Content-Disposition: form-data; name="upload_file"; filename="C:\testing\myfile.da<WBR>­t" Content-Type: application/octet-stream")
12
5928
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: <gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish> etc. I want to remove the "g:\" from the file paths. I wrote a console app that successfully reads the file and writes a duplicate of it, but fails for some reason to do the "replacing" of the "g:\". The code...
7
6063
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
68
5257
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
10
22744
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) outFile.write("teststring", 10); outFile.close(); If I leave out the ios::ate and ios::app modes my string is written to the start of the file as I'd expect but I want to write the data to
16
4500
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 executable?
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.