operator>>(std::basic_istream) - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# operator>>(std::basic_istream)

From cppreference.com
< cpp‎ | io‎ | basic istream

template< class CharT, class Traits > basic_istream<CharT,Traits>& operator>>( basic_istream<CharT,Traits>& st, CharT& ch ); template< class Traits > basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& st, signed char& ch ); template< class Traits > basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& st, unsigned char& ch ); | (1) |
template< class CharT, class Traits> basic_istream<CharT,Traits>& operator>>( basic_istream<CharT,Traits>& st, CharT* s ); template< class Traits > basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& st, signed char* s ); template< class Traits > basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& st, unsigned char* s ); | (2) |
| (3) |
template< class CharT, class Traits, class T > basic_istream<CharT,Traits>& operator>>( basic_istream<CharT,Traits>&& st, T& value ); |  | (since C++11) (until C++17)
template< class CharT, class Traits, class T > basic_istream<CharT,Traits>& operator>>( basic_istream<CharT,Traits>&& st, T&& value ); |  | (since C++17)

Performs character input operations.

1) Behaves as an `FormattedInputFunction`. After constructing and checking the sentry object, which may skip leading whitespace, extracts a character and stores it to `ch`. If no character is available, sets `failbit` (in addition to `eofbit` that is set as required of a `FormattedInputFunction`).

2) Behaves as an `FormattedInputFunction`. After constructing and checking the sentry object, which may skip leading whitespace, extracts successive characters and stores them at successive locations of a character array whose first element is pointed to by `s`. The extraction stops if one of the following conditions are met:

  - a whitespace character (as determined by the `ctype<CharT>` facet) is found. The whitespace character is not extracted.
  - st.width() - 1 characters are extracted
  - end of file occurs in the input sequence (this also sets `eofbit`)

In either case, an additional null character value CharT() is stored at the end of the output. If no characters were extracted, sets `failbit` (the null character is still written, to the first position in the output). Finally, calls st.width(0) to cancel the effects of std::setw, if any.

3)

Calls the appropriate extraction operator, given an rvalue reference to an input stream object (equivalent to st >> value). | (since C++11) (until C++17)
Calls the appropriate extraction operator, given an rvalue reference to an input stream object (equivalent to st >> std::forward<T>(value)). This function does not participate in overload resolution unless the expression st >> std::forward<T>(value) is well-formed. | (since C++17)

### [edit] Notes

Extracting a single character that is the last character of the stream does not set `eofbit`: this is different from other formatted input functions, such as extracting the last integer with operator>>, but this behavior matches the behavior of std::scanf with "%c" format specifier.

### [edit] Parameters

st | - | input stream to extract the data from
ch | - | reference to a character to store the extracted character to
s | - | pointer to a character string to store the extracted characters to

### [edit] Return value

`st`

### [edit] Example

Run this code

    #include <iostream>
    #include <iomanip>
    #include <sstream>

    int main()
    {
        std::string input = "n greetings";
        std::istringstream stream(input);
        char c;
        const int MAX = 6;
        char cstr[MAX];

        stream >> c >> std::setw(MAX) >> cstr;
        std::cout << "c = " << c << '\n'
                  << "cstr = " << cstr << '\n';

        double f;
        std::istringstream("1.23") >> f; // rvalue stream extraction
        std::cout << "f = " << f << '\n';
    }

Output:

    c = n
    cstr = greet
    f = 1.23

### [edit] See also

operator>> | extracts formatted data (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_istream/operator_gtgt2&oldid=91985"
Hidden category:

- Pages with unreviewed LWG DR marker