1

I am trying to write a code, where I need to store the intermediate results in a file for later retrieval. For example, I need to store all the values in an 3D Array in a file, with the dimensions and later retrieve the array for later use. Is there a way I could preserve the object for later use, by storing in a file. For Example....

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;

Dont look too much details in the code snippet. its just an example..

Thanks... I think there is another think called binary serialization...

howtechstuffworks
  • 1,824
  • 4
  • 29
  • 46

2 Answers2

1

It's indeed called serialization. You're best off not reinventing the wheel. Use Boost.Serialization instead.

MSalters
  • 173,980
  • 10
  • 155
  • 350
-1
ofstream file("some_file.bin", ios::binary);
file.write(array, sizeof(array));
file.close();
Michael Dorst
  • 8,210
  • 11
  • 44
  • 71