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?

Recommended Answers

All 2 Replies

I would write lines 3 and 4 like:

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

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;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.