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?
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;
Is this your file name or file content? I assume it's file content.
#include<fstream>
#include<string>
//..........
ifstream fin;
fin.open("your file", ifstream::in);
string var1, var2, var3;
fin>> var 1 >> var2 >> var 3;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With