开发者

File operation in C++

开发者 https://www.devze.com 2023-01-11 05:31 出处:网络
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 g

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 v开发者_运维知识库alue.


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

Is it possible to serialize and deserialize a class in C++?

0

精彩评论

暂无评论...
验证码 换一张
取 消