問題描述
std::cout <<<有什么情況嗎?你好"
不起作用?我有一個 c/c++ 代碼,但是 std::cout
不打印任何內(nèi)容,甚至不打印常量字符串(例如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
是否能夠/無法打開流?有一些成員函數(shù),比如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.
推薦答案
確保刷新流.這是必需的,因為輸出流是緩沖的,除非您自己手動刷新緩沖區(qū),否則您無法保證何時刷新緩沖區(qū).
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
將只是進行刷新.也可以使用流的成員函數(shù)來完成刷新:
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 不會打印的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!