【问题标题】:Can we store an object in a file for later retrieval?我们可以将对象存储在文件中以供以后检索吗?
【发布时间】:2012-03-26 00:57:13
【问题描述】:

我正在尝试编写代码,我需要将中间结果存储在文件中以供以后检索。例如,我需要将 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;

代码sn-p不要看太多细节。 这只是一个例子..

谢谢... 我觉得还有一种思路叫做二进制序列化……

【问题讨论】:

  • 使用二进制序列化有多好???我在里面使用了很多指针......
  • 措辞不佳,与旧问题完全相同。我建议搜索类似的主题,因为这已经回答过。

标签: c++ file serialization fstream


【解决方案1】:

确实叫序列化。你最好不要重新发明轮子。请改用Boost.Serialization

【讨论】:

    【解决方案2】:
    ofstream file("some_file.bin", ios::binary);
    file.write(array, sizeof(array));
    file.close();
    

    【讨论】:

      猜你喜欢
      • 2013-06-23
      • 2015-02-23
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 2021-04-22
      • 2016-03-18
      相关资源
      最近更新 更多