Namespaces
Variants
Views
Actions

std::basic_ostream::put

From cppreference.com
basic_ostream& put( char_type ch );

Writes character ch to the output stream.

This function is an unformatted output function: it begin execution by constructing an object of type sentry, which flushes the tie()'d output buffers if necessary and checks the stream errors. After construction, if the sentry object returns false, the function returns without attempting any output. If an exception is thrown during output, then ios::badbit is set (the exception is suppressed unless exceptions()&badbit) != 0, in which case it is rethrown)

Contents

[edit] Parameters

ch - character to write

[edit] Return value

*this

[edit] Notes

This function is not overloaded for the types signed char or unsigned char, unlike the formatted operator<<

Unlike formatted output functions, this function does not set the failbit if the output fails.

[edit] Example

#include <fstream>
#include <iostream>
int main()
{
    std::cout.put('a'); // normal usage
    std::cout.put('\n');
 
    std::ofstream s("/does/not/exist/");
    s.clear(); // pretend the stream is good
    std::cout << "Unformatted output: ";
    s.put('c'); // this will set badbit, but not failbit
    std::cout << " fail=" << bool(s.rdstate() & s.failbit);
    std::cout << " bad=" << s.bad() << '\n';
    s.clear();
    std::cout << "Formatted output:   ";
    s << 'c'; // this will set badbit and failbit
    std::cout << " fail=" << bool(s.rdstate() & s.failbit);
    std::cout << " bad=" << s.bad() << '\n';
}

Output:

a
Unformatted output:  fail=0 bad=1
Formatted output:    fail=1 bad=1

[edit] See also

inserts character data
(function) [edit]
inserts blocks of characters
(public member function) [edit]