C ++ reading file to place

Is there a way to read data from a file before 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 the final variable. How to do this with ifstream?

+7
source share
2 answers

You can simply read the values ​​into std :: string variables, this will automatically tokenize them.

 std::string fName, middleInit, lName; my_stream >> fName >> middleInit >> lName; 
+12
source

Is this your file name or file? I assume this is the contents of the file.

 #include<fstream> #include<string> //.......... ifstream fin; fin.open("your file", ifstream::in); string var1, var2, var3; fin>> var 1 >> var2 >> var 3; 
+1
source

All Articles