Strings to binary files

前端 未结 2 1080
隐瞒了意图╮
隐瞒了意图╮ 2021-01-28 18:36

My problem goes like this: I have a class called \'Register\'. It has a string attribute called \'trainName\' and its setter:

class Register {

 private:
    str         


        
2条回答
  •  迷失自我
    2021-01-28 18:40

    You cannot write string this way, as it almost certainly contains pointers to some structs and other binary stuff that cannot be serialized at all. You need to write your own serializing function, and write the string length + bytes (for example) or use complete library, for example, protobuf, which can solve serializing problem for you.

    edit: see praetorian's answer. much better than mine (even with lower score at time of this edit).

提交回复
热议问题