Namespaces
Variants
Views
Actions

std::basic_istream::ignore

From cppreference.com
< cpp‎ | io‎ | basic istream
Revision as of 20:04, 10 January 2013 by Nate (Talk | contribs)

Template:ddcl list begin <tr class="t-dcl ">

<td class="t-dcl-nopad">
basic_istream& ignore( std::streamsize count = 1, int_type delim = Traits::eof() );
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Extracts and discards characters from the input stream until and including delim.

ignore behaves as an UnformattedInputFunction. After constructing and checking the sentry object, it extracts characters from the stream and discards them until any one of the following conditions occurs:

  • end of file conditions occurs in the input sequence, in which case the function calls setstate(eofbit)
  • the next available character c in the input sequence is delim, as determined by Traits::eq_int_type(Traits::to_int_type(c), delim). The delimiter character is extracted and discarded. This test is disabled if delim is Traits::eof()

Contents

Parameters

count - number of characters to extract
delim - delimiting character to stop the extraction at. It is also extracted.

Return value

*this

Example

The following example uses ignore to skip over non-numeric input:

#include <iostream>
#include <sstream>
#include <limits>
 
int main()
{
    std::istringstream input("1\n"
                             "some non-numeric input\n"
                             "2\n");
    for(;;) {
        int n;
        input >> n;
 
        if (input.eof() || input.bad()) {
            break;
        } else if (input.fail()) {
            input.clear(); // unset failbit
            input.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // skip bad input
        } else {
            std::cout << n << '\n';
        }
    }
}

Output:

1
2

See also

Template:cpp/io/basic istream/dcl list getTemplate:cpp/io/basic istream/dcl list getline