c++ - how to parse the following file -


i have little issue transfer python code c++ have file format like:

============================== there    0   element ============================== there    62  element 1vtz0   13  196 196 13  196 196 184 2520    2buka   1   1vtz0   1   195 1vtz1   13  196 196 13  196 196 184 2520    2buka   1   1vtz1   1   195 1vtz2   13  196 196 13  196 196 184 2350    2buka   1   1vtz2   1   195 1vtz3   13  196 196 13  196 196 184 2470    2buka   1   1vtz3   1   195 1vtz4   13  196 196 13  196 196 184 2560    2buka   1   1vtz4   1   195 1vtz5   13  196 196 13  196 196 184 2340    2buka   1   1vtz5   1   195 1vtz6   13  196 196 13  196 196 184 3320    2buka   1   1vtz6   1   195 1vtz7   13  196 196 13  196 196 184 2820    2buka   1   1vtz7   1   195 1vtze   13  196 196 13  196 196 184 2140    2buka   1   1vtze   1   195 

what want skip line ===================== there 0 element. in original python code need use if condition:

 if (not "====="  in line2) , (len(line2)>30): 

i used similar if condition in c++ still can not rid of "element" line, c++ code following:

 unsigned  pos1, pos2;  string needle1="element"  string needle2="====="  infile.open(filename.c_str(), ios::in);  while (!infile.eof())  {      if(line==" ")     {        cout<<"end of file" <<endl;        break;     }      getline(infile, line);     pos1=line.find(needle1);       if (pos1!=string::npos&& pos2!=string::npos && (line.length()> 40))     {         ...........     } 

thanks!

i don't particularly know why you're checking line lengths, there better ways this.

for example, index string (as it's array of characters), , use continue statement inside while loop.

while (!infile.eof()){       if (line[0] == '=') { // assumes no other lines start '='       continue;     }    if(line==" "){      cout<<"end of file" <<endl;      break;   }    // whatever need non "===... xx element" lines 

}

http://www.cplusplus.com/reference/string/string/operator[]/ http://www.learncpp.com/cpp-tutorial/58-break-and-continue/


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -