I have written an object to file but when I read the object from the file, it is not getting the correct value. If 1 object is considered as 1 record and if only 1 record is there in the file, it's getting the value. If I write more records (many objects of the same type) then it's not getting the value.
Asked
Active
Viewed 334 times
-2
-
8Showing your code would help. – sje397 Aug 11 '10 at 06:50
-
Maybe you try to open a file multi times, that can't be done. When a file is opened, you must close it before open it a gain. – Bang Dao Aug 11 '10 at 06:53
-
1http://www.cplusplus.com/doc/tutorial/files/. Have a read and try figuring out. – DumbCoder Aug 11 '10 at 07:29
-
1 suggestion without seeing any code: have a look at the file, does it contain the data as expected? – stefaanv Aug 11 '10 at 08:54
1 Answers
1
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).

Community
- 1
- 1

John Rocha
- 1,656
- 3
- 19
- 30