?
Solved

Handling errors in getline

Posted on 2003-11-08
2
Medium Priority
?
690 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
efn earned 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

765 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