std::basic_ostream<CharT,Traits>::put
From cppreference.com
< cpp | io | basic ostream
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Input/output library
Print functions (C++23)
Buffers
(C++23)
(C++98/26*)
(C++20)
Streams
Abstractions
File I/O
String I/O
Array I/O
(C++23)
(C++23)
(C++23)
(C++98/26*)
(C++98/26*)
(C++98/26*)
Synchronized Output
(C++20)
Types
Error category interface
(C++11)
(C++11)
std::basic_ostream
Global objects
Member functions
(C++11)
Formatted output
Unformatted output
basic_ostream::put
Positioning
Miscellaneous
(C++11)
Member classes
Non-member functions
(C++23)
(C++23)
(C++23)
(C++23)
basic_ostream& put( char_type ch );
Behaves as an UnformattedOutputFunction. After constructing and checking the sentry object, writes the character ch
to the output stream.
If the output fails for any reason, sets badbit
.
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
Run this code
#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