0

I'm reading a file in line by line. At the end of the file there wackily are to carriage returns. I'm reading the lines in like this (code below) then parsing them. Because there are two wonderful returns the wonderful program crashes everytime I run it.

        char* ptr;
        ptr = strtok(buffer," ");
        if(testFile.eof());
        testFile.getline(buffer, strlen(buffer));

Typically this worked for me, but alas, doesn't do dick when I use it now

 if(buffer[i] == EOF) break;

It's one of those situations where this wonderful program is due tomorrow and I spent all day dicking around with the sort and search functions (which still don't fully work, I don't think) but this crashing problem is driving me nut, any suggestions?

3
Contributors
2
Replies
13
Views
3 Years
Discussion Span
Last Post by Lucaci Andrew
0

I would write lines 3 and 4 like:

 if(!testFile.eof())
 {
    testFile.getline(buffer, strlen(buffer));
 }
0

You could do it using the ifstream class Click Here.
Here's a small example:

string filename = "a.txt";
ifstream fin(filename.c_str(), ios::in);
if (fin.good()){
    string line = "";
    while (getline(fin, line)){
        cout << line << endl;
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.