标准输出并需要刷新它 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6214629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
stdout and need to flush it C++
提问by Dixon Steel
I have some C++ code that uses cout
statements for debug purposes and for some reason I can't get all the data to print unless I do a std::cout.flush();
at the end.
我有一些 C++ 代码将cout
语句用于调试目的,出于某种原因,除非我std::cout.flush();
在最后执行 a ,否则我无法打印所有数据。
I don't quite understand why this flush operation is needed.
我不太明白为什么需要这个刷新操作。
Anyone have any insight?
任何人都有任何见解?
采纳答案by Matteo Italia
To add to the other answers: your debugging statements should instead go to cerr
, because:
要添加到其他答案:您的调试语句应该转到cerr
,因为:
- it writes to the standard error, which means that, running the application, you can easily separate the "normal" program output from the errors/debug information via redirection;
- most importantly,
cerr
by default is unbuffered, which means that, after each output operation, it will automatically flush itself, and in general this is desirable for errors and debug output.
- 它写入标准错误,这意味着在运行应用程序时,您可以通过重定向轻松地将“正常”程序输出与错误/调试信息分开;
- 最重要的是,
cerr
默认情况下是unbuffered,这意味着,在每次输出操作后,它会自动刷新自身,通常这对于错误和调试输出是可取的。
(source: C++ standard, §27.3.1 ?4-5, §27.4.2.1.2 table 83)
(来源:C++ 标准,§27.3.1 ?4-5,§27.4.2.1.2 表 83)
回答by James Kanze
Are you using std::endl
to terminate your lines. This should be the
usual practice, until performance issues require otherwise, but for some
reason, I see a lot of code which uses '\n'
instead.
您是否正在使用std::endl
终止您的线路。这应该是通常的做法,除非性能问题另有要求,但出于某种原因,我看到很多代码都在使用'\n'
。
Otherwise, you can always do:
否则,您可以随时执行以下操作:
std::cout.setf( std::ios_base::unitbuf );
as one of the first things in main
. This will cause a flush at the
end of every <<
, which is more than you need, but for diagnostic
output to the console, is probably quite acceptable.
作为main
. 这将导致在 every 结束时刷新<<
,这超出了您的需要,但对于控制台的诊断输出,可能是完全可以接受的。
回答by mah
Is the data that isn't automatically getting flushed lacking a \n
at the end? By default, standard out doesn't get delivered until a carriage return is seen.
未自动刷新的数据\n
最后是否缺少 a ?默认情况下,在看到回车符之前不会传递标准输出。
回答by Christian Neverdal
"When you send output to a stream, it does not necessarily get printed immediately. Rather, it may wait in a buffer until some unspecified event, e.g. buffer full enough, reading from input, or exit from program. The details may vary."
“当您将输出发送到流时,它不一定会立即打印出来。相反,它可能会在缓冲区中等待一些未指定的事件,例如缓冲区足够满、从输入读取或从程序退出。细节可能会有所不同。”
http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200109/notes/io.html
http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200109/notes/io.html
回答by Alessandro Teruzzi
It is the right behavior. You probably use std::endl
that add \n
and flush the buffer. http://www.cplusplus.com/reference/ostream/endl/
这是正确的行为。您可能会使用std::endl
添加\n
和刷新缓冲区。http://www.cplusplus.com/reference/ostream/endl/
You need to flush the stream, if you want to see the output.
如果要查看输出,则需要刷新流。
回答by Stuti
In C++ you can use endl
formatter with cout
operator rather then flush
.
在 C++ 中,您可以将endl
格式化程序与cout
运算符一起使用,而不是flush
.
回答by mike
The answer of std::endl
is only valid if you want a return.
Not sure how you would do this if you wanted to flush a command prompt out.
的答案std::endl
仅在您想要退货时才有效。如果您想刷新命令提示符,不确定如何执行此操作。