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?

Dani AI

Generated

A few things stand out from 's description that explain the crash and how to fix it cleanly. First, a conditional that has a stray semicolon becomes a no‑op and can let a read happen past EOF. Second, passing strlen(buffer) as the space limit to a read is unsafe: strlen looks at the buffer contents (which may be uninitialized) instead of the buffer’s capacity, and that can cause overruns. Third, comparing characters to EOF is incorrect — EOF is an int sentinel used by some input functions, not a valid char value. ’s reminder to guard reads and ’s suggestion to use C++ streams are both on the right track, but you still need to handle trailing CRs and empty lines before parsing.

Prefer reading into a std::string, strip any trailing carriage return, skip empty lines, then tokenize with a safe parser (for example, an istringstream). Two small helpers that are immediately useful:

inline void stripCR(std::string &s) {
    if (!s.empty() && s.back() == '\r') s.pop_back();
}
std::istringstream iss(line);
std::string token;
while (iss >> token) {
    // process token safely
}

Practical checklist to stop the crashes:

  • Remove the stray semicolon after any if(...) checks so the condition actually matters.
  • Use std::getline into a std::string (or pass the actual buffer capacity if you must use a C buffer).
  • After each read, remove a trailing '\r' and continue on empty lines before parsing.
  • Don’t compare char values to EOF; instead rely on the success/failure of the read operation.
  • Replace strtok with std::istringstream or token‑handling that won’t modify shared buffers.

If the problem persists, add a tiny debug print that dumps the numeric byte values of the last few characters on each line — that will show stray CR/LF bytes and confirm which of the above issues is occurring.

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.