44

Is there any circumstance when std::cout << "hello" doesn't work? I have a C++ program where std::cout doesn't seem to print anything, not even constant strings (such as "hello").

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.

starball
  • 20,030
  • 7
  • 43
  • 238
mahmood
  • 23,197
  • 49
  • 147
  • 242

4 Answers4

62

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 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();
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • 5
    @GrijeshChauhan Since when does sending just a newline char to a formatted output stream force a flush? I thought the standard was not content-aware about flushes, relying on members (`flush()`) and manipulators (`endl`) to trip a stream buffer flush? Has it been like this since a specific C++ standard? – WhozCraig Feb 13 '13 at 16:37
  • @GrijeshChauhan That doesn't flush the buffer (or shouldn't: on some implementations it does). – James Kanze Feb 13 '13 at 16:40
  • Please post the group of statements containing the std::cout – Subhajit Feb 13 '13 at 16:41
  • @WhozCraig I have not read anywhere but I tried sometime like this. Its worked. – Grijesh Chauhan Feb 13 '13 at 16:41
  • 3
    @GrijeshChauhan Then if it does flush on your implementation, it does so outside the standard. It it isn't guaranteed to on all implementations. If you want a guaranteed newline+flush, you need `std::endl` or append a `\n` and then invoke one of a number of members that will trip a flush. Just appending text with a `'\n'` won't be guaranteed by the standard to do it alone. – WhozCraig Feb 13 '13 at 16:43
  • Sorry for the confusion. Actually in the makefile I wrote -O3. In this mode we can not debug the code, so the cursor in the IDE malfunctioned. As a result the cout didn't write immediately even with flush. The flush is now fine. Thanks – mahmood Feb 13 '13 at 16:44
  • 1
    For completeness only (sftrabbit's proposed solutions are better), but you can call `std::cout.rdbuf()->pubsetbuf( NULL, 0 );` to turn off buffering. Or you can do `std::cout << std::unitbuf` to activate unit buffering. – James Kanze Feb 13 '13 at 16:45
  • @GrijeshChauhan in Linux/Unix world \n works fine, but in windows/DOS `\n\r` or `\r\n` flush the system that is why it is good idea to use std::endl which does exactly that –  May 12 '17 at 00:55
16

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

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.

Jan Paulmann
  • 181
  • 1
  • 6
10

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.

Nicolay77
  • 2,085
  • 25
  • 20
  • 1
    This helped me to fix stdout which stopped printing after some amount (random) of data transferred. flush() did not help there. Thanks a lot. – kyb Apr 06 '20 at 07:43
9

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

You can do one of these things:

  • flush std::cout explicitly:

    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::cerr instead. std::cerr is 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").

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
utnapistim
  • 26,809
  • 3
  • 46
  • 82