C++ Read File Until Space

Howdy_McGee picture Howdy_McGee · Feb 14, 2012 · Viewed 19.2k times · Source

Is there a way I can read data from a file until a space? I have a file

John J. Doe

and I want to read the file and put John in 1 variable, J. in another variable and Doe in a final variable. How do I do this with ifstream?

Answer

Jesus Ramos picture Jesus Ramos · Feb 14, 2012

You can just read the values into std::string variables, this will automatically tokenize it.

std::string fName, middleInit, lName;
my_stream >> fName >> middleInit >> lName;