Writing to and Reading back from typed (struct) file that struct has String field #2501
Unanswered
LukyGuyLucky
asked this question in
Q&A
Replies: 1 comment 2 replies
-
|
If you use write_any, then you store the struct by value, the struct contains two strings and one int. The strings contain a pointer value and a length each. In other words you are saving the addresses rather than the actual strings. The fact that it works the first time around is simply because you are loading back pointer values that are still valid. What you need in order to properly serialize and deserialize is a custom function for it. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
As an exercise,I wrote a tiny program trying to understand something related to typed files and String vs. fixed length char array.
In below program,only the first run can print the only record just added with write_struct() from the file,not a second chance.While I changed the String field to fixed length char array in the struct Student,then it seems everything is fine.I don't quite understand why String field failed on such case.
Another issue is ,when reading back record from the file using while(!file.eof()) and io::read_any(&file,&alice) as marked Option B in the source, after the while loop finished,an unexpected fault happened but I don't know why.
Beta Was this translation helpful? Give feedback.
All reactions