std::cout won't print
Asked Answered
H

4

49

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.

Harber answered 13/2, 2013 at 16:31 Comment(3)
Ah, buffered-output, though art a heartless and cruel wench.Anisette
In a windowing system, the std::cout may not be implemented because there are windows and the OS doesn't know which one of your windows to output to.Sidsida
never ever give cout NULL. it will stop to work.Cleo
S
65

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();
Sky answered 13/2, 2013 at 16:32 Comment(10)
@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?Anisette
@GrijeshChauhan That doesn't flush the buffer (or shouldn't: on some implementations it does).Anastassia
Please post the group of statements containing the std::coutDeglutinate
@Anisette I have not read anywhere but I tried sometime like this. Its worked.Watchtower
@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.Anisette
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. ThanksHarber
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.Anastassia
@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 thatShaman
i have the same issue with @mahmood. But for me nothing seems to work. I am using windows 11, and run the executable in powershell. When the programm meets the line with the << operator it exits. Even with a simple std::cout<< "hello" << endf; it stopsNeuro
Am using gcc compiler. Added "-static" flag and it worked. See this so ansShakta
M
20

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.

Maryannamaryanne answered 17/12, 2018 at 10:29 Comment(4)
More specifically, you need OutputDebugString.Baptistery
One also needs to add "_CONSOLE" in "C/C++->Preprocessor->Preprocessor Definitions", as mentioned here: https://mcmap.net/q/193482/-how-do-i-print-to-the-debug-output-window-in-a-win32-appJaclynjaco
@rodrigocfd: OutputDebugString() is unrelated to std::cout. OutputDebugString() is a completeley different device than console in Windows. std::cout goes to the classic text console (DOS prompt), while OutputDebugString() goes to a debug port that Windows applications can hook in like the MS Visual Studio IDE. Using MS Visual Studio the difference between std::cout and OutputDebugString() might not become obvious because both outputs will display in the "output" log view.Maryannamaryanne
Edit: In MS Visual Studio the std::cout outputs will not display in the "output" log view. (While I was typing my comment above, I pressed ENTER by mistake which sent the unfinished comment without any question. Editing was not possible 5 minutes after. How can I changes these Stackoverflow settings?)Maryannamaryanne
M
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.

Methuselah answered 26/5, 2016 at 14:30 Comment(2)
This helped me to fix stdout which stopped printing after some amount (random) of data transferred. flush() did not help there. Thanks a lot.Crag
very strange, so far I tried std::endl, std::flush, stdbuf, std::setvbuf, still after a while the print to stdout is gone, however I can still interact with the program which means it is running, just the stdout is not displayingHadfield
G
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").

Goodin answered 13/2, 2013 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.