Reading from file, checking contents of a line

Pages: 12
The problem is that argv[i+1] contains chars and the char '2' has not the value 2. You can convert the char by unsing atoi:

if( ss == atoi(argv[i+1] )
ss is a stringstream, argv[i+1] is a pointer to char.
You should also convert argv[i+1] to a number

1
2
3
4
5
6
7
if(ss >> line_number)
{
    cout << "line: " << line << ", line_number: " << line_number
        << ", argv[(i + 1)]: " << argv[(i + 1)] << endl;
    if(line_number == atoi ( argv[(i + 1)] ) ) // Notice: the number you are comparing is 'line_number', not 'ss'
        cout << line << endl;
}
atoi ( http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/ ) is a C library function, if you want to be more C++, you should use another stringstream or the boost library
http://www.cplusplus.com/forum/articles/9645/

EDIT: mordekai typed faster than me
Last edited on
Wow, thanks a lot guys, il try this tomorrow (got to go out now)

Thanks very much for all your replies, il let you know if I have any other problems :)
And Bazzy is correct. You should compare line_number, not ss, to atoi( argv[i+1] )
dont use eof, use:
 
while(getline(myFile,line,'~');

your could search each line by using a counter. If there is a word match, save the counter into a new variable and display that line, from there you are only manipulating the string.
Topic archived. No new replies allowed.
Pages: 12