首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_ostream::put

basic_ostream& put( char_type ch );

表现为UnformattedOutputFunction.在构造和检查哨兵对象之后,编写字符ch到输出流。

如果输出因任何原因失败,则设置badbit...

参数

ch

-

character to write

返回值

*this...

注记

此函数不会对类型重载。signed charunsigned char,与格式化的运算符<<

与格式化输出函数不同,此函数不设置failbit如果输出失败。

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
a
Unformatted output:  fail=0 bad=1
Formatted output:    fail=1 bad=1

二次

另见

operator<<(std::basic_ostream)

inserts character data (function template)

write

inserts blocks of characters (public member function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券