tags:

views:

108

answers:

1

I have written an object to file but when i read the object from the file is not getting the correct value. if 1 object is considered as 1 record if only 1 record is there in the file its getting the value. if i write more record (many object of same type) then its not getting the value. plz help me what to do.

+1  A: 

There could be a couple of problems. Of the top of my head it might be an issue with how you are interfacing with the file, as the comments above indicate. Or it might be a serialization issue.

I recently learned that one cannot simply write a class to a file because its not guaranteed to be contiguous memory (other reasons too, but the moral I took was don't do it).

Instead one needs to serialize the object/class before writing it to the file.

A couple of web resources that I found useful for this are:

http://functionx.com/cpp/articles/serialization.htm (I liked this one a lot).

http://functionx.com/cpp/articles/serialization.htm

http://stackoverflow.com/questions/234724/how-to-serialize-in-c

John Rocha