c++ - read data from a file with multiple sections from VC++ in VS2013 -
i need read txt file vc++ in vs2013.
in file, there mutiple sections:
#section1 head1,head2,head3 dcscsa, sdew, safce ..... #section2 head1,head2,head3, head4,head5 112,633,788,632,235 .....
i need save lines different data structure section1: mapsection1>
section12 mapsection2<string, map<string, int>>
can use code :
string aline; getline(file, aline); stringstream ss(aline); int cnt = 0; if (file.good()) { while (!file.eof()) { string substr; getline(file, aline); stringstream ss(aline); while (ss.good()) { // how save data different map different section? }
also, can load whole file data set , process each line or process each line when file read line line.
which 1 more efficient ?
thanks
can use code : ...
not quite.
use:
int sectionno = 0; while (getline(file, aline)) { if (aline.empty()) continue; if (aline[0] == '#') { ++sectionno; getline(file, aline); // read headings... use them if continue; } std::stringstream ss(aline); switch (sectionno) { case 1: if (ss >> >> b >> c >> std::skipws && ss.eof()) ... use a, b, c ... else throw std::runtime_error("invalid data in section 1 " + aline); break; case 2: ... }
also, can load whole file data set , process each line or process each line when file read line line.
which 1 more efficient ?
almost latter.
Comments
Post a Comment