tags:

views:

2946

answers:

2

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.cc:13: error: request for member 'find' in 'tableEntry', which is of non-class type 'int'

+4  A: 

You try to extract an integer from the line, and then try to find a "#" in the integer. This doesn't make sense, and the compiler complains that there is no find method for integers.

You probably should check the "#" directly on the read line at the beginning of the loop. Besides that you need to declare qlline and actually open the file somewhere and not just pass a string with it's name to getline. Basically like this:

ifstream myfile("myfile.txt");
string qlline;
while (getline(myfile, qlline)) {
  if (qlline.find("#") == 0) {
    continue;
  }
  ...
}
sth
+6  A: 

Is this 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;
}
CTT
I would also suggest trimming the line before checking for comment characters.
Hosam Aly
You're right, I've edited the example to trim the start of the string.
CTT