std::cout won#39;t print(std::cout 不会打印)
问题描述
std::cout <<<有什么情况吗?你好" 不起作用?我有一个 c/c++ 代码,但是 std::cout 不打印任何内容,甚至不打印常量字符串(例如hello").
Is there any circumstance when std::cout << "hello" doesn't work? I have a c/c++ code, however the std::cout doesn't print anything, not even constant strings (such as "hello").
有什么方法可以检查 cout 是否能够/无法打开流?有一些成员函数,比如good()、bad(),...但我不知道哪个适合我.
Is there any way to check if cout is able/unable to open the stream? There are some member functions like good(), bad(), ... but I don't know which one is suitable for me.
推荐答案
确保刷新流.这是必需的,因为输出流是缓冲的,除非您自己手动刷新缓冲区,否则您无法保证何时刷新缓冲区.
Make sure you flush the stream. This is required because the output streams are buffered and you have no guarantee over when the buffer will be flushed unless you manually flush it yourself.
std::cout << "Hello" << std::endl;
std::endl 将输出一个换行符并刷新流.或者,std::flush 将只是进行刷新.也可以使用流的成员函数来完成刷新:
std::endl will output a newline and flush the stream. Alternatively, std::flush will just do the flush. Flushing can also be done using the stream's member function:
std::cout.flush();
这篇关于std::cout 不会打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::cout 不会打印
基础教程推荐
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
