C++ 我们可以将对象存储在文件中以供以后检索吗?
我正在尝试编写一个代码,需要将中间结果存储在一个文件中,以便以后检索。例如,我需要将三维数组中的所有值存储在一个文件中,并带有尺寸标注,然后检索数组以供以后使用。有没有一种方法可以通过存储在文件中来保存对象以备将来使用。例如C++ 我们可以将对象存储在文件中以供以后检索吗?,c++,file,serialization,fstream,C++,File,Serialization,Fstream,我正在尝试编写一个代码,需要将中间结果存储在一个文件中,以便以后检索。例如,我需要将三维数组中的所有值存储在一个文件中,并带有尺寸标注,然后检索数组以供以后使用。有没有一种方法可以通过存储在文件中来保存对象以备将来使用。例如 class Obj { }; Obj ***array; //declare and initialize the 3d Array. . . . //do some modifications . . . . write the object in the file o
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;
类Obj{
};
Obj***阵列;
//声明并初始化三维数组。
.
.
.
//做一些修改
.
.
.
.
在文件中写入对象
流文件;
打开(“some_file.txt”);
另一个数组;
不要在代码片段中查看太多细节。
这只是一个例子
谢谢。。。
我认为还有另一种想法叫做二进制序列化…它实际上叫做序列化。你最好不要重新发明轮子。改为使用。使用二进制序列化的好处可能有多大???我在里面用了很多指针……措词不当,完全是老问题的翻版。我建议搜索类似的主题,因为这已经得到了回答。
ofstream file("some_file.bin", ios::binary);
file.write(array, sizeof(array));
file.close();