25

Is there a way to catch the stdout and stderr in Visual Studio? For example, when I use

cout <<"Hello world!"<< endl;

A black window appears and disappears. It's so fast that I can't see it. There is a output section in the IDE but it only allow me to choose display output from build and something else but without the choice of stdout.

A cheating solution maybe calling

system("pause");

but it doesn't sound right. I searched in the option but I can't find an item.

Anyone has any idea? Thanks. I just start to use VS and I'm on Linux before.

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
YankeeWhiskey
  • 1,522
  • 4
  • 20
  • 32
  • Have a look at the answers here: http://stackoverflow.com/questions/1343320/when-debugging-on-windows-where-does-stderr-go – Russ Clarke Apr 19 '12 at 23:43

4 Answers4

11

Rather than using the "Start Debugging" command, if you want to check the output, use "Start Without Debugging". That's generally how I handle it. The command prompt window will remain up until you close it. If there's a crash or something, you can always reattach Visual Studio to the process, or just run it with "Start Debugging".

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
9

Go to Project Properties -> Linker -> System -> Change subsystem to Console.

Iceman
  • 4,202
  • 7
  • 26
  • 39
  • I changed that but the black window keeps popping up. Where am I suppose to see the output in VS? – YankeeWhiskey Apr 19 '12 at 23:48
  • 4
    Output will be in the console which pops up. It won't disappear if you go to Debug -> Start without Debugging. – Iceman Apr 19 '12 at 23:49
  • Not working! Got linking error after this change. `unresolved external symbol main`. Vs 2019, MFC application Signle Document – user123445151156 Dec 09 '20 at 14:30
  • I have set it to Console but the Output window (with "Show output from Debug" selected) does not contain any messages. VS2019 – Rye bread Apr 28 '21 at 12:03
3

Without actually trying to redirect stdin/err streams, there are some alternatives you can try:

  • Open a command prompt window and cd to the directory your executable will be created in then run it from there.
  • Perhaps try using something like std::cin.get(); or std::cin.ignore(std::cin.rdbuf()->in_avail() + 1); instead of system("pause").
  • Display output in the visual studio debugger via OutputDebugString
thelazydeveloper
  • 3,598
  • 2
  • 19
  • 12
-2

Changing subsystem to console doesn't seem to work with an MFC dialog application.

DocDJ
  • 31
  • 5
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33869750) – user16217248 Feb 21 '23 at 23:27