了解二进制文件。 [英] Understanding binary files.

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

问题描述

我正在编写一个程序,我正在努力学习并保存二进制

文件。这是我作为来源找到的页面:
http://www.angelfire.com/country/ald...aryFileIO.html


我已经成功创建并使用了txt文件。


我试图保存然后加载一个指向对象的指针数组:


class Map {


int xlen ;

int ylen;


int mapTotal;


HWND hwnd;


空间* playArea;

grTerrain grTerr;

....


地图::地图(HWND h ,const int x,const int y){


xlen = x;

ylen = y;


hwnd = h;


playArea = new space [(xlen * ylen)];

create();

}

void Map :: load(std :: ifstream& f){


f.seekg(0);

f。 read((char *)& mapTotal,sizeof(int));

f.read((char *)& playArea,sizeof(space) * mapTotal);

MessageBox(NULL,Loading ...,模拟,MB_OK);

}


void Map :: save(std :: ofstream& f){

f.seekp(0);

f.write((char *)& mapTotal,sizeof(mapTotal));

f.write((char *)& playArea,sizeof(space));


MessageBox(NULL," Saving ..."," Simulation", MB_OK);

}

案例ID_LOAD:{

std :: ifstream f(" data.bin",ios :: in | ios :: binary);

board-> load(f);

f.close();

InvalidateRect(hwnd,NULL ,FALSE);


休息;

}


案例ID_SAVE:{

std :: ofstream f(" data.bin",ios :: out | ios :: binary);

board-> save(f);

f.close();

休息;

}


我发布了适用于保存的程序部分

loading。该程序确实保存并创建了一个bin文件53kb,所以它好像数据正在保存,但是与txt文件不同,很难确定信息是否正确保存。

解决方案

JoeC写道:


我正在编写一个程序我正在尝试学习并保存二进制

文件。这是我作为来源找到的页面:
http://www.angelfire.com/country/ald...aryFileIO.html


我已经成功创建并使用了txt文件。


我试图保存然后加载一个指向对象的指针数组:


[..]

void Map :: load (std :: ifstream& f){


f.seekg(0);

f.read((char *)& mapTotal,sizeof( INT));



奇怪的不对称。您使用''sizeof(mapTotal)''编写,但是您使用''sizeof(int)''读取了
。为什么?


f.read((char *)& playArea,sizeof(space)* mapTotal);



另一个奇怪的不对称(可能是逻辑错误)。你看了

''mapTotal''空格,买你只写了一个(见下文)。


MessageBox(NULL," Loading) ..."," Simulation",MB_OK);

}


void Map :: save(std :: ofstream& f){

f.seekp(0);

f.write((char *)& mapTotal,sizeof(mapTotal));

f。 write((char *)& playArea,sizeof(space));



如果你的对象中有''mapTotal''空格,你为什么只写

一个?


>

MessageBox(NULL," Saving ..."," Simulation",MB_OK);

}

[..]


我发布了适用于保存和

加载的程序部分。该程序确实保存并创建了一个bin文件53kb,所以它好像数据正在保存,但是与txt文件不同,很难确定信息是否正确保存。



只是出于好奇的缘故,你有问题吗?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要''请问




" JoeC" < en ***** @ yahoo.com写信息

news:11 ********************** @ n76g2000hsh。 googlegr oups.com ...


>我正在编写一个程序,我正在努力学习并保存二进制

文件。这是我作为来源找到的页面:
http://www.angelfire.com/country/ald...aryFileIO.html


我已经成功创建并使用了txt文件。


我试图保存然后加载一个指向对象的指针数组:


class Map {


int xlen ;

int ylen;


int mapTotal;


HWND hwnd;


space * playArea;

grTerrain grTerr;

...


地图::地图(HWND h, const int x,const int y){


xlen = x;

ylen = y;


hwnd = h;


playArea =新空间[(xlen * ylen)];

create();

}


void Map :: load(std :: ifstream& f){


f.seekg(0);

f.read((char *)& mapTotal,sizeof(int));

f.read((char *)& playArea,sizeof (空格)* mapTotal);

MessageBox(NULL,Loading ...,Simulation",MB_OK);

}


void Map :: save(std :: ofstream& f){

f.seekp(0);

f.write((char *)& mapTotal,sizeof(mapTotal));

f.write((char *)& playArea,sizeof(space));


MessageBox(NULL," Saving ..."," Simulation", MB_OK);

}


案例ID_LOAD:{

std :: ifstream f(" data.bin",ios :: in | ios :: binary);

board-> load(f);

f.close();

InvalidateRect (hwnd,NULL,FALSE);


休息;

}


案例ID_SAVE:{

std :: ofstream f(" data.bin",ios :: out | ios :: binary);

board-> save(f);

f.close();

休息;

}


我发布了适用的程序部分节省和

加载。该程序确实保存并创建了一个bin文件53kb,所以它好像数据正在保存,但是与txt文件不同,很难确定信息是否正确保存。



您是否遇到了特定问题?或者您是否希望有人b $ b某人错误检查您的所有代码?或者您是否在询问如何验证它已保存?

后者,然后加载它并将加载的数据与

数据进行比较,然后保存...不确定你的确切问题是什么。


3月28日上午10点34分,JoeC < enki ... @ yahoo.comwrote:


我正在编写一个程序,我正在努力学习并保存二进制文件

文件。这是我作为来源找到的页面: http:// www.angelfire.com/country/ald...aryFileIO.html



< snip>


我发布了适用于保存和

加载的程序部分。该程序确实保存并创建了一个bin文件53kb,所以它好像数据正在保存,但是与txt文件不同,很难确定信息是否正确保存。



我将假设问题是我如何确保这是正常的?
正确吗?"


基本上,你应该弄清楚你期望的结果,并且

将它们与实际结果进行比较。


例如,让我们'假设你有一个4字节长的int,其值为
,值为0x01020304。如果您对文件执行二进制转储(例如,使用

在UNIX下的'od''程序或将其加载到Visual Studio中的适当二进制

查看器中或无论如何),你会期望看到

字节01,02,03,04,或者可能是04,03,02,01。


我会建议创建一个具有一些愚蠢价值的小测试用例

(例如,01020304),并手动确认它存储了你想要的b
。我会特别小心指针部分,因为它已经很难正确地序列化指针。


和FYI,会有这个组中的一些人告诉你,

二进制输出是魔鬼的工作,而且该文本是适用于所有交换的适当媒体。因为,据我所知,你的b $ b目标是学习如何做二进制输出,忽略它们。


Michael


I am writing a program that I am trying to learn and save binary
files. This is the page I found as a source:
http://www.angelfire.com/country/ald...aryFileIO.html

I have successfully created and used txt files.

I am trying to save then load in an array of pointers to objects:

class Map{

int xlen;
int ylen;

int mapTotal;

HWND hwnd;

space * playArea;
grTerrain grTerr;
....

Map::Map(HWND h, const int x, const int y){

xlen = x;
ylen = y;

hwnd = h;

playArea = new space[(xlen * ylen)];
create();
}
void Map::load(std::ifstream& f){

f.seekg (0);
f.read((char*)&mapTotal, sizeof(int));
f.read((char*)&playArea, sizeof(space) * mapTotal);
MessageBox(NULL, "Loading...", "Simulation", MB_OK);
}

void Map::save(std::ofstream& f){
f.seekp (0);
f.write((char*)&mapTotal, sizeof(mapTotal));
f.write((char*)&playArea, sizeof(space));

MessageBox(NULL, "Saving...", "Simulation", MB_OK);
}
case ID_LOAD: {
std::ifstream f("data.bin", ios::in | ios::binary);
board->load(f);
f.close();
InvalidateRect(hwnd,NULL,FALSE);

break;
}

case ID_SAVE:{
std::ofstream f("data.bin", ios::out | ios::binary);
board->save(f);
f.close();
break;
}

I posted the parts of the program that apply to the saving and
loading. The program does save and creates a bin files 53kb so it
seems like the data is saving but unlike txt files, it is hard to
verify if the informations saved correctly.

解决方案

JoeC wrote:

I am writing a program that I am trying to learn and save binary
files. This is the page I found as a source:
http://www.angelfire.com/country/ald...aryFileIO.html

I have successfully created and used txt files.

I am trying to save then load in an array of pointers to objects:

[..]
void Map::load(std::ifstream& f){

f.seekg (0);
f.read((char*)&mapTotal, sizeof(int));

Strange assymetry. You write using ''sizeof(mapTotal)'', but you read
using ''sizeof(int)''. Why?

f.read((char*)&playArea, sizeof(space) * mapTotal);

Another strange assymetry (and probably a logical error). You read
''mapTotal'' spaces, buy you only wrote one (see below).

MessageBox(NULL, "Loading...", "Simulation", MB_OK);
}

void Map::save(std::ofstream& f){
f.seekp (0);
f.write((char*)&mapTotal, sizeof(mapTotal));
f.write((char*)&playArea, sizeof(space));

If there are ''mapTotal'' spaces in your object, why do you only write
one?

>
MessageBox(NULL, "Saving...", "Simulation", MB_OK);
}
[..]

I posted the parts of the program that apply to the saving and
loading. The program does save and creates a bin files 53kb so it
seems like the data is saving but unlike txt files, it is hard to
verify if the informations saved correctly.

And just for the curiousity''s sake, did you have a question?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



"JoeC" <en*****@yahoo.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...

>I am writing a program that I am trying to learn and save binary
files. This is the page I found as a source:
http://www.angelfire.com/country/ald...aryFileIO.html

I have successfully created and used txt files.

I am trying to save then load in an array of pointers to objects:

class Map{

int xlen;
int ylen;

int mapTotal;

HWND hwnd;

space * playArea;
grTerrain grTerr;
...

Map::Map(HWND h, const int x, const int y){

xlen = x;
ylen = y;

hwnd = h;

playArea = new space[(xlen * ylen)];
create();
}
void Map::load(std::ifstream& f){

f.seekg (0);
f.read((char*)&mapTotal, sizeof(int));
f.read((char*)&playArea, sizeof(space) * mapTotal);
MessageBox(NULL, "Loading...", "Simulation", MB_OK);
}

void Map::save(std::ofstream& f){
f.seekp (0);
f.write((char*)&mapTotal, sizeof(mapTotal));
f.write((char*)&playArea, sizeof(space));

MessageBox(NULL, "Saving...", "Simulation", MB_OK);
}
case ID_LOAD: {
std::ifstream f("data.bin", ios::in | ios::binary);
board->load(f);
f.close();
InvalidateRect(hwnd,NULL,FALSE);

break;
}

case ID_SAVE:{
std::ofstream f("data.bin", ios::out | ios::binary);
board->save(f);
f.close();
break;
}

I posted the parts of the program that apply to the saving and
loading. The program does save and creates a bin files 53kb so it
seems like the data is saving but unlike txt files, it is hard to
verify if the informations saved correctly.


Are you running into a particular problem? Or are you looking to have
someone bug-check all your code? Or are you asking how to verify it saved?
It the latter, then load it back up and compare the loaded data with the
data before the save...Not sure what your exact question is.


On Mar 28, 10:34 am, "JoeC" <enki...@yahoo.comwrote:

I am writing a program that I am trying to learn and save binary
files. This is the page I found as a source:http://www.angelfire.com/country/ald...aryFileIO.html

<snip>

I posted the parts of the program that apply to the saving and
loading. The program does save and creates a bin files 53kb so it
seems like the data is saving but unlike txt files, it is hard to
verify if the informations saved correctly.

I will assume the question is "how do I make sure this is working
correctly?"

Basically, you should figure out what results you are expecting, and
compare those to the actual.

For example, let''s say you have an int that is 4 bytes long, with a
value of 0x01020304. If you do a binary dump of the file (e.g., with
the ''od'' program under UNIX or loading it into an appropriate binary
viewer in Visual Studio or whatever), you would expect to see the
bytes 01, 02, 03, 04, or possibly 04, 03, 02, 01.

I would recommend creating a small test case with some goofy values
(e.g., 01020304) and confirming, by hand, that it''s storing what you
think. I''d be especially careful with the pointer part, as it is
known to be tricky to serialize pointers correctly.

And FYI, there will be some people on this group who tell you that
binary output is the work of the devil, and that text is the
appropriate medium for all exchanges. Since, as I understand it, your
goal is to learn how to do binary output, ignore them.

Michael


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

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