Solved

Handling errors in getline

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

706 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

14 Experts available now in Live!

Get 1:1 Help Now