二进制文件 [英] binary file

查看:86
本文介绍了二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在开发一个开源项目,我遇到了一个

二进制文件的问题。当我从sourceforge下载程序下载的二进制文件

时,我没有问题。但是,我无法使用Microsoft Visual C ++编译的程序读取这个

二进制文件

Express Edition SP1。程序从文件读取错误的值并且

崩溃。我不明白是因为:

*从sourceforge下载的程序是用Microsoft编译的

Visual C ++(2004年)

* it不是大/小端的问题。

* CI / O函数和C ++ I / O类从文件中读取相同的错误值。

文件。


有什么想法吗?

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?

推荐答案

2007-08-23 13:06,Ludo写道:
On 2007-08-23 13:06, Ludo wrote:



我正在开发一个开源项目,我遇到了问题

二进制文件。当我从sourceforge下载程序下载的二进制文件

时,我没有问题。但是,我无法使用Microsoft Visual C ++编译的程序读取这个

二进制文件

Express Edition SP1。程序从文件读取错误的值并且

崩溃。我不明白是因为:

*从sourceforge下载的程序是用Microsoft编译的

Visual C ++(2004年)

* it不是大/小端的问题。

* CI / O函数和C ++ I / O类从文件中读取相同的错误值


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.



如果没有看到您的代码,我们就无法确定。但是,有些b / b
在C ++语言或编译器中没有任何东西阻止你读取二进制文件。我可以看到这个问题的两个可能原因,

首先是你使用的其他程序对数据执行某种类型的修改,或者你尝试的方式读取数据是错误的。

当你打开它时,你是否指定该文件应该作为二进制文件读取(ios_base :: binary)?


-

Erik Wikstr?m

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


如果没有看到您的代码,我们就无法确定。


这是从文件中读取数据的函数:

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 = n ew long [mNumber];

if(mpData == NULL)

{mNumber = 0;

返回false;

}

mNumber = fread(mpData,sizeof(long),mNumber,fp);


ResetDataRange();

返回true;

}


这个用于将数据写入文件:


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);


返回true;

}
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;
}

但是,那里有
在C ++语言或编译器中没有任何东西可以阻止你使用

读取bi没有文件。我可以看到这个问题的两个可能原因,

首先是你使用的其他程序对数据执行某种类型的修改,或者你尝试的方式读取数据是错误的。
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.



我编译了程序,其中包含从

sourceforge下载的源文件,无需修改。

I have compiled the program with the source files downloaded from
sourceforge without modification.


当你打开它时,你是否指定该文件应该作为二进制文件读取(ios_base :: binary)?
Did you specify that the file should be read as a binary file when
opening it (ios_base::binary)?



是的:FILE * fp = fopen(mFileName," rb");

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


8月23日晚上7:06,Ludo< lmout1 ... @ yahoo.frwrote:
On Aug 23, 7:06 pm, Ludo <lmout1...@yahoo.frwrote:



我正在开发一个开源项目,我有一个

二进制文件的问题。当我从sourceforge下载程序下载的二进制文件

时,我没有问题。但是,我无法使用Microsoft Visual C ++编译的程序读取这个

二进制文件

Express Edition SP1。程序从文件读取错误的值并且

崩溃。我不明白是因为:

*从sourceforge下载的程序是用Microsoft编译的

Visual C ++(2004年)

* it不是大/小端的问题。

* CI / O函数和C ++ I / O类从文件中读取相同的错误值。

文件。


有什么想法吗?
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?



我曾从sourceforge下载过一些源代码。我认为这是关于VC版本的问题。

有时候我下载了用VS2003编写的源代码,我不能

编译它在VS2005中。

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.


这篇关于二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆