Search code examples
c++ifstream

C++ Read File Until Space


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?


Solution

  • 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;