2012-03-26 94 views
1

我想寫一個代碼,我需要將中間結果存儲在文件中供以後檢索。例如,我需要將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

可能的重複[如何在C++中實現序列化](http://stackoverflow.com/questions/1809670/how-to-implement-serialization-in-c) – 2012-03-26 00:59:44

+0

使用二進制序列化有多好?我在裏面使用了大量的指針...... – howtechstuffworks 2012-03-26 01:05:51

+0

糟糕的措辭和老問題的確切副本。我建議搜索類似的話題,因爲這已經被回答過。 – 2012-03-26 02:48:10

回答

-1
ofstream file("some_file.bin", ios::binary); 
file.write(array, sizeof(array)); 
file.close(); 
相關問題