Solved

Handling errors in getline

Posted on 2003-11-08
2
685 Views
Last Modified: 2011-09-20
I am able to load a file just fine with the following:

    char HoldLineData[300];
    ifstream fin(LOAD_FILENAME);

    while (!fin.eof()) {
        fin.getline(HoldLineData,300);
            // Process line here
        }
    }

However, if the streamsize is greater than 300 in one particular line, naturally getline will fail, and nasty things happen to the processing of the line.  So, I check for a failure as follow:

    char HoldLineData[300];
    ifstream fin(LOAD_FILENAME);

    while (!fin.eof()) {
        fin.getline(HoldLineData,300);
            if (fin.fail()) {
                fin.clear();
            } else {
                // Process line here
            }
        }
    }

This code runs fine, providing there is data in the final line of the program.  However, if the final line of the file is is empty, eof is not reached and my program crashes.  

Should I be using a different method of extracting data, or have I made an error in my code?
0
Comment
Question by:derekpapesch
2 Comments
 
LVL 15

Accepted Solution

by:
efn earned 50 total points
ID: 9707147
What I think is going on:

When the last line of input is non-empty, reading it sets eof and doesn't set fail, so the line gets processed and the loop terminates.

When the last line is empty, reading it sets both eof and fail.  The code then clears both eof and fail and so the loop continues indefinitely, reading, failing, and clearing.

I don't see how it would crash, but maybe this kind of loop is what you meant.  Or maybe your library works differently from the one with which I tested (Visual C++ 6.0).

To fix it, you could change the fail test to:

            if (fin.fail() && !fin.eof()) {

--efn
0
 
LVL 4

Author Comment

by:derekpapesch
ID: 9709331
Ahhh!  That is so logical, It didn't occur to me that eof was also cleared by my clear().  

You're right, it is a infinite loop, I was crashing out of the ap.  

Thanks a million!
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question