Home > OS >  I am using ifstream and I need to read in two words as one string instead of splitting them
I am using ifstream and I need to read in two words as one string instead of splitting them

Time:05-21

I need to read in a line from a file, but I need to save two strings as one. For example, if the file's line had someone's name like Bernie Sanders, I would want to save the entire name into a string variable instead of just the first name.

CodePudding user response:

I assume you are doing

  std::string nameString;
  fs >> nameString;

this will read to the end of a word. do this instead

 std::string nameString;
 std::getline(fs, nameString);

this will read the whole line

  • Related