From: xRxIxPx <xr...@gm...> - 2003-09-28 08:26:34
|
At 23:19 09/27/03 -0700, chea sin ong wrote: >does anyone know can i use the following code? >i want to write something into a binary file. the i >will use another to read it? can anyone tell me if i >can use the following in dev C++? i picked this >example from a book but i can write the file but can't >read from the binary file. That is, because you cannot just save every bit of an object (unless you are able to recreate the same runtime state on load as it was on save, but this is not a concept unless you are the os). The std::string will break your neck, because it uses dynamic allocated memory and pointers to it. When you save your file, you will save only a pointer value to a memory address which is no more valid when loading. You will have to create a write/read member function for your class, which uses the stream to save and load itself. >2 different files are as below: > >... >struct person >{ >protected : >string name; >int age ; >.... >file.write(reinterpret_cast<char*>(&pers),sizeof(pers)); > >.... >struct person >{ >protected : >string name; >int age ; >.... > >file.read(reinterpret_cast<char*>(&pers),sizeof(pers)); >.... |