Solved

Handling errors in getline

Posted on 2003-11-08
2
683 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

896 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now