Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

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

I'm wanting to save the content of a file to a struct. I've tried to use seekg and read to write to it but it isn't working.

My file is something like:

johnmayer24ericclapton32

I want to store the name, the last name and the age in a struct like that

typedef struct test_struct{
    string name;
    string last_name;
    int age;
} test_struct;

Here is my code

int main(){

    test_struct ts;
    ifstream data_base;

    data_base.open("test_file.txt");

    data_base.seekg(0, ios_base::beg);
    data_base.read(ts, sizeof(test_struct));

    data_base.close();

    return 0;
}

It doesn't compile as it don't want me to use ts on the read function. Is there another way - or a way - of doing it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 '' if some string might contain that character; recall that std::string can store ''. 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 ' ' or '"' or even '' 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 ' ') 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 ' ', 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...