c++ getline and stringstream -
i'm trying read in file, has 5 lines, , every line 3-4 string long. here's input file:
10:30 hurley 1234567a 10:15 10:45 hurley 1234567a 11:30 08:35 jacob 1x1x1x1x1x 08:35 jacob 1x1x1x1x1x 08:10 08:05 jacob 1x1x1x1x1x 08:45 sayid 33332222 09:15
and get:
10:30 hurley 1234567a 10:15 10:45 hurley 1234567a 11:30 08:35 jacob 1x1x1x1x1x 11:30 08:35 jacob 1x1x1x1x1x 08:10 08:05 jacob 1x1x1x1x1x 08:10 08:45 sayid 33332222 09:15
this code:
void enor::read(status &sx,isle &dx,ifstream &x){ string str; getline(x, str, '\n'); stringstream ss; ss << str; ss >> dx.in >> dx.name >> dx.id >> dx.out; /*getline(x, str, '\n'); x>>dx.in>>dx.name>>dx.id>>dx.out;*/ if(x.fail()) sx=abnorm; else sx=norm; }
how can read in file without having 3rd , 5th line filled 2nd , 4th line's time? want dx.out empty. should use method, or possible done stringstream?
if >>
sees there nothing left in stringstream
, leave variable untouched - dx.out
keeps value last line. however, can do
ss >> dx.in >> dx.name >> dx.id; if (!(ss >> dx.out)) dx.out = "";
because ss >> dx.out
returns ss
, , when stream converted bool
(such when used in if
condition), returns false
if last read attempt failed.
Comments
Post a Comment