Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Read File Until Space

Tags:

c++

ifstream

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?

like image 687
Howdy_McGee Avatar asked Feb 14 '12 02:02

Howdy_McGee


2 Answers

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;
like image 60
Jesus Ramos Avatar answered Sep 23 '22 23:09

Jesus Ramos


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;
like image 23
YankeeWhiskey Avatar answered Sep 22 '22 23:09

YankeeWhiskey