Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am using the ifstream function in c++ and I need to save two words as one string instead of splitting them

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading