C++ std::cout 不会打印

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14858262/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:45:06  来源:igfitidea点击:

std::cout won't print

c++debuggingbufferiostreamcout

提问by mahmood

Is there any circumstance when std::cout << "hello"doesn't work? I have a c/c++ code, however the std::coutdoesn't print anything, not even constant strings (such as "hello").

std::cout << "hello"没有不工作的情况?我有 ac/c++ 代码,但是std::cout它不打印任何东西,甚至不打印常量字符串(例如“hello”)。

Is there any way to check if coutis 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.

有什么方法可以检查是否cout能够/无法打开流?有一些成员函数,例如good(), bad(), ... 但我不知道哪个适合我。

回答by Joseph Mansfield

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::endlwill output a newline and flush the stream. Alternatively, std::flushwill justdo the flush. Flushing can also be done using the stream's member function:

std::endl将输出换行符并刷新流。或者,std::flush进行冲洗。也可以使用流的成员函数完成刷新:

std::cout.flush();

回答by utnapistim

It is probable that std::coutdoesn't work due to buffering (what you're writing ends up in the buffer of std::coutinstead of in the output).

std::cout由于缓冲(您所写的内容最终在缓冲区中std::cout而不是在输出中),这可能不起作用。

You can do one of these things:

您可以执行以下操作之一:

  • flush std::coutexplicitly:

    std::cout << "test" << std::flush; // std::flush is in <iostream>
    

    std::cout << "test";
    std::cout.flush(); // explicitly flush here
    

    std::cout << "test" << std::endl; // endl sends newline char(s) and then flushes
    
  • use std::cerrinstead. std::cerris not buffered, but it uses a different stream (i.e. the second solution may not work for you if you're interested in more than "see message on console").

  • std::cout明确冲洗:

    std::cout << "test" << std::flush; // std::flush is in <iostream>
    

    std::cout << "test";
    std::cout.flush(); // explicitly flush here
    

    std::cout << "test" << std::endl; // endl sends newline char(s) and then flushes
    
  • 使用std::cerr来代替。std::cerr没有缓冲,但它使用不同的流(即,如果您对“查看控制台上的消息”感兴趣,则第二种解决方案可能不适合您)。

回答by Jan Paulmann

std::cout won't work on GUI apps!

std::cout 不适用于 GUI 应用程序!

Specific to MS Visual Studio: When you want a console application and use MS Visual Studio, set project property "Linker -> System -> SubSystem" to Console. After creating a new Win32 project (for a native C++ app) in Visual Studio, this setting defaults to "Windows" which prevents std::cout from putting any output to the console.

特定于 MS Visual Studio:当您需要控制台应用程序并使用 MS Visual Studio 时,将项目属性“链接器 -> 系统 -> 子系统”设置为控制台。在 Visual Studio 中创建新的 Win32 项目(用于本机 C++ 应用程序)后,此设置默认为“Windows”,以防止 std::cout 将任何输出放入控制台。

回答by Nicolay77

To effectively disable buffering you can call this:

要有效地禁用缓冲,您可以调用它:

std::setvbuf(stdout, NULL, _IONBF, 0);

Alternatively, you can call your program and disable output buffering in the command line:

或者,您可以调用您的程序并在命令行中禁用输出缓冲:

stdbuf -o 0 ./yourprogram --yourargs

Keep in mind this is not usually done for performance reasons.

请记住,出于性能原因,通常不会这样做。