c++ - Test for end of line and white space -
i reading file of 5 doubles in line through loop , storing them vector of structs , there less 5 doubles on line. file looks like
111.111 222.222 333.333 444.444 555.555
666.666
777.777 (whitespace) 888.888 999.999
struct temp_struct{ double d1,d2,d3,d4,d5 }; vector<temp_struct> data; for(int i=0;i<3;i++) file>>data.at(i).d1>>data.at(i).d2>>data.at(i).d3>>data.at(i).d4>>data.at(i).d5;
is there way test empty space after 666.666 , inbetween 777.777 , 888.888.
because right way reading in, after 666.666 go next line of file , read 777.777 , 888.888 999.999 struct looks data.at(1).d1=666.666 data.at(1).d2=777.777 data.at(1).d3=888.888 , data.at(1).d4=999.999 rather have
data.at(1).d1=666.666, break because of end of line data.at(2).d1=777.777, data.at(2).d2=0, data.at(3).d3=888.8888
i programming in vs2010 c++
the usual way parse line-based text use std::getline()
in combination std::istringstream
, e.g.:
for (std::string line; std::getline(in, line); ) { std::istringstream lin(line); // parse line using `line` }
there other approaches, too, though. example, possible set stream not consider newline whitespace: way, trying read numbers when newline encountered attempt read value fail! way change considered whitespace install custom std::locale
object custom version of std::ctype<char>
.
Comments
Post a Comment