How to skip reading a line in a file in C ++?

The file contains the following data:

#10000000 AAA 22.145 21.676 21.588 10 TTT 22.145 21.676 21.588 1 ACC 22.145 21.676 21.588 

I tried to skip lines starting with "#" using the following code:

 #include <iostream> #include <sstream> #include <fstream> #include <string> using namespace std; int main() { while( getline("myfile.txt", qlline)) { stringstream sq(qlline); int tableEntry; sq >> tableEntry; if (tableEntry.find("#") != tableEntry.npos) { continue; } int data = tableEntry; } } 

But for some reason, it gives this error:

Mycode.cc13: error: query for member 'find' in 'tableEntry' that is of type non-class 'int'

+4
source share
2 answers

Does it look more like what you want?

 #include <iostream> #include <sstream> #include <fstream> #include <string> #include <algorithm> using namespace std; int main() { fstream fin("myfile.txt"); string line; while(getline(fin, line)) { //the following line trims white space from the beginning of the string line.erase(line.begin(), find_if(line.begin(), line.end(), not1(ptr_fun<int, int>(isspace)))); if(line[0] == '#') continue; int data; stringstream(line) >> data; cout << "Data: " << data << endl; } return 0; } 
+9
source

You are trying to extract an integer from a string, and then try to find the "#" in integers. This makes no sense, and the compiler complains that there is no find method for integers.

You should probably check the "#" directly on the read line at the beginning of the loop. In addition, you need to declare qlline and actually open the file somewhere, and not just pass the line with that name to getline . Mostly like this:

 ifstream myfile("myfile.txt"); string qlline; while (getline(myfile, qlline)) { if (qlline.find("#") == 0) { continue; } ... } 
+4
source

All Articles