[Solved] How to save text file to struct with string in C++


Serialization/Deserialization of strings is tricky.

As binary data the convention is to output the length of the string first, then the string data.

https://isocpp.org/wiki/faq/serialization#serialize-binary-format

  • String data is tricky because you have to unambiguously know when the string’s body stops. You can’t unambiguously terminate all strings with a ‘\0’ if some string might contain that character; recall that std::string can store ‘\0’. The easiest solution is to write the integer length just before the string data. Make sure the integer length is written in “network format” to avoid sizeof and endian problems (see the solutions in earlier bullets).

That way when reading the data back in you know the length of the string to expect and can preallocate the size of the string then just read that much data from the stream.

If your data is a non-binary (text) format it’s a little trickier:

https://isocpp.org/wiki/faq/serialization#serialize-text-format

  • String data is tricky because you have to unambiguously know when the string’s body stops. You can’t unambiguously terminate all strings with a ‘\n’ or ‘”‘ or even ‘\0’ if some string might contain those characters. You might want to use C++ source-code escape-sequences, e.g., writing ‘\’ followed by ‘n’ when you see a newline, etc. After this transformation, you can either make strings go until end-of-line (meaning they are deliminated by ‘\n’) or you can delimit them with ‘”‘.
  • If you use C++-like escape-sequences for your string data, be sure to always use the same number of hex digits after ‘\x’ and ‘\u’. I typically use 2 and 4 digits respectively. Reason: if you write a smaller number of hex digits, e.g., if you simply use stream << “\x” << hex << unsigned(theChar), you’ll get errors when the next character in the string happens to be a hex digit. E.g., if the string contains ‘\xF’ followed by ‘A’, you should write “\x0FA”, not “\xFA”.
  • If you don’t use some sort of escape sequence for characters like ‘\n’, be careful that the operating system doesn’t mess up your string data. In particular, if you open a std::fstream without std::ios::binary, some operating systems translate end-of-line characters.
    Another approach for string data is to prefix the string’s data with an integer length, e.g., to write “now is the time” as 15:now is the time. Note that this can make it hard for people to read/write the file, since the value just after that might not have a visible separator, but you still might find it useful.

Text-based serialization/deserialization convention varies but one field per line is an accepted practice.

1

solved How to save text file to struct with string in C++