Why does reading a record struct fields from std::istream fail, and how can I fix it?

后端 未结 9 2117
野性不改
野性不改 2020-11-21 11:40

Suppose we have the following situation:

  • A record struct is declared as follows

struct Person {
    unsigned int id;
    std::st         


        
9条回答
  •  执念已碎
    2020-11-21 12:31

    What can I do to read in the separate words forming the name into the one actRecord.name variable?

    The general answer is: No, you can't do this without additional delimiter specifications and exceptional parsing for the parts forming the intended actRecord.name contents.
    This is because a std::string field will be parsed just up to the next occurence of a whitespace character.

    It's noteworthy that some standard formats (like e.g. .csv) may require to support distinguishing blanks (' ') from tab ('\t') or other characters, to delimit certain record fields (which may not be visible at a first glance).

    Also note:
    To read an uint8_t value as numeric input, you'll have to deviate using a temporary unsigned intvalue. Reading just a unsigned char (aka uint8_t) will screw up the stream parsing state.

提交回复
热议问题