我们可以将对象存储在文件中以便以后检索吗?

时间:2012-03-26 00:57:14

标签: c++ file serialization fstream

我正在尝试编写代码,我需要将中间结果存储在一个文件中以便以后检索。例如,我需要将3D数组中的所有值存储在文件中,并使用维度,然后检索该数组以供以后使用。有没有办法通过存储在文件中来保存对象供以后使用。例如......

class Obj {
};

Obj ***array;
//declare and initialize the 3d Array.
.
.
.
//do some modifications
.
.
.
.
write the object in the file
ofstream file;
file.open("some_file.txt");
file<<array;
.
.
.
end program.

reopen another program
ifstream file;
file.open("some_file.txt");
Obj *another_array;
file>>another_array;

请勿在代码段中查看太多详细信息。 这只是一个例子..

...谢谢 我认为还有另一种想法称为二元序列化......

2 个答案:

答案 0 :(得分:1)

它的确称为序列化。你最好不要重新发明轮子。请改用Boost.Serialization

答案 1 :(得分:-1)

ofstream file("some_file.bin", ios::binary);
file.write(array, sizeof(array));
file.close();
相关问题