我有一个对象列表(类型为Radio_Data),我需要能够存储到文件并从文件中恢复。我使用以下代码将类保存在二进制文件中:
for (listIter = listCont.begin(); listIter != listCont.end(); listIter++)
{
file.seekg(iter * sizeof(Radio_Data), ios::beg);
file.write((char *)&listIter, sizeof(Radio_Data));
}
并尝试使用此代码恢复它们:
for(counter = 0; counter < (fileSize/sizePerEntry); counter++)
{
file.seekg((counter) * sizeof(Radio_Data), ios::beg);
file.read((char *)&station, sizeof(Radio_Data));
}
我可以在文件中看到输入的信息,所以我知道保存效果很好。但是,当尝试从文件读回信息时,会显示不正确的数据。我做错了什么?
class Radio_Data
{
private:
char name[21];
char callsign[5];
char band[3];
char frequency[6];
char type[21];
char city[21];
char phone[11];
char website[41];
public:
Radio_Data();
~Radio_Data(){};
//More public methods
}
以下是保存文件的样子: 0-F x-F鹰派,棉衣,FM 1039,包装,棉衣,棉衣,棉衣,棉衣,棉衣,棉衣 ÌÌÌÌ5198608520www.thehawk.caÌÌÌÌÌÌ (有一个TAB字符b-n 0-F和x-F)
此文件应解析为:
char name[21] == "the hawk";
char callsign[5] == "CKDK";
char band[3] == "FM";
char frequency[6] == "1039";
char type[21] == "rock";
char city[21] == "Woodstock";
char phone[11] == "5198608520";
char website[41] == "www.thehawk.ca";
提前致谢!
答案 0 :(得分:2)
在Visual Studio 2012 和 OpenBSD g ++版本4.2.1上尝试了以下(快速和脏)代码并且它有效...
#include <iostream>
#include <fstream>
class Radio_Data
{
public:
char name[21];
char callsign[5];
char band[3];
char frequency[6];
char type[21];
char city[21];
char phone[11];
char website[41];
public:
Radio_Data() {}
~Radio_Data(){}
//More public methods
};
void set(Radio_Data* obj)
{
strcpy(obj->name, "the hawk");
strcpy(obj->callsign, "CKDK");
strcpy(obj->band, "FM");
strcpy(obj->frequency, "1039");
strcpy(obj->type, "rock");
strcpy(obj->city, "Woodstock");
strcpy(obj->phone, "5198608520");
strcpy(obj->website, "www.thehawk.ca");
}
void clear(Radio_Data* obj)
{
memset(obj->name, 0, sizeof(obj->name));
memset(obj->callsign, 0, sizeof(obj->callsign));
memset(obj->band, 0, sizeof(obj->band));
memset(obj->frequency, 0, sizeof(obj->frequency));
memset(obj->type, 0, sizeof(obj->type));
memset(obj->city, 0, sizeof(obj->city));
memset(obj->phone, 0, sizeof(obj->phone));
memset(obj->website, 0, sizeof(obj->website));
}
void prnt(Radio_Data* obj)
{
std::cout << "---start object---" << std::endl;
std::cout << obj->name << std::endl;
std::cout << obj->callsign << std::endl;
std::cout << obj->band << std::endl;
std::cout << obj->frequency << std::endl;
std::cout << obj->type << std::endl;
std::cout << obj->city << std::endl;
std::cout << obj->phone << std::endl;
std::cout << obj->website << std::endl;
std::cout << "----end object----" << std::endl;
}
void write(Radio_Data* obj)
{
std::ofstream o("file.txt");
o.write((char*)obj, sizeof(Radio_Data));
o.close();
}
void read(Radio_Data* obj)
{
std::ifstream i("file.txt");
i.read((char*)obj, sizeof(Radio_Data));
i.close();
}
int main(int argc, char** argv)
{
Radio_Data obj;
clear(&obj);
prnt(&obj);
set(&obj);
prnt(&obj);
write(&obj);
clear(&obj);
prnt(&obj);
read(&obj);
prnt(&obj);
return 0;
}
它在控制台中输出了两个:
---start object---
----end object----
---start object---
the hawk
CKDK
FM
1039
rock
Woodstock
5198608520
www.thehawk.ca
----end object----
---start object---
----end object----
---start object---
the hawk
CKDK
FM
1039
rock
Woodstock
5198608520
www.thehawk.ca
----end object----
这是文件内容:
视窗:
the hawk CKDK FM 1039 rock Woodstock 5198608520 www.thehawk.ca
和OpenBSD:
the hawkCKDKFM1039rockWoodstock5198608520www.thehawk.ca
所以你的代码中似乎还有其他错误。