Thread: Stream - understand failbit

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    30

    Stream - understand failbit

    Hi all,

    i have created a text file:
    Code:
    100
    200
    three hundred
    400
    my goal is to output at the end : 100, 200, 400. which is worked with the following code:
    Code:
    while (true) {
                if (input >> num) {
                    cout << num << " - ";
                }
    
    
                else if (input.eof()) {
                    break;
                }
    
    
                else if (input.fail()) {
                    input.clear();
                    input.ignore(numeric_limits<streamsize>::max(), '\n');
                }
            }
    but if i have a text file like this:
    Code:
    100
    200
    three 300
    400
    how should i modify the ignore function, that it does not discard the whole line , but only the word "three", so that i output : 100, 200, 300, 400

  2. #2
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    You could use getline() to read each line, split it by spaces, and then a stringstream object to extract valid values.

    The real question though is why are you even trying to make sense of bad input? Seems to me it would make more sense to report an error and exit in such cases.

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    30
    Thanks for the hints, i will try it
    i am not trying to make sense of anything, it was just for the sake of exercise (how would i solve if i had to )

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Scatman
    i am not trying to make sense of anything, it was just for the sake of exercise (how would i solve if i had to )
    That is what you're trying to do though: make sense of the input according to certain rules. When you're just allowing for one number per line, it is easy to be lax, but if you're trying to interpret a number on a line that may contain other things rather than just considering that to be invalid input, you should be more careful. The thing then is to be absolutely clear as to what are those rules. For example, what about:
    Code:
    three300
    or
    Code:
    three300zero
    or
    Code:
    three 300 five 500
    Do you allow negative numbers?

    So, depending on what rules you want to use, the exact approach you take to parsing for the number might differ.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is a stream? What really is a stream object?
    By Ramcin Oudishu in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2014, 01:33 PM
  2. Failbit trouble
    By OldSchool in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2006, 09:12 PM
  3. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM
  4. goodbit and failbit of cin
    By Raison in forum C++ Programming
    Replies: 21
    Last Post: 03-05-2004, 02:31 PM
  5. Usage of badbit and failbit
    By golfinguy4 in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2003, 09:43 AM

Tags for this Thread