Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++: No output from cout object


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 JohnsonGPS   Members   -  Reputation: 109

Like
0Likes
Like

Posted 28 April 2009 - 12:51 PM

I am using Visual C++ 2008 Express Edition, and I am new to it. Just to test the water, I wrote a simple Windows Form application. I also use cout object to output some strings. When I debugged it in IDE, I saw no string being outputted to the debug window of the IDE. When I ran the application from command windows, I didn't see any outputs in the command windows as well. So where the strings go? Any thought? Thank you! The codes are very simple: #include <iostream> using namespace std; void CTest::Dump (void) { cout << "\r\n Test the water!"; }

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9417

Like
1Likes
Like

Posted 28 April 2009 - 01:06 PM

By default std::cout outputs to the standard output handle. However, an application running in the Windows subsystem doesn't have standard out hooked up to anything. You could create a console with AllocConsole() and then redirect standard out to that console window. Ex:

AllocConsole();
freopen("conin$","r",stdin);
freopen("conout$","w",stdout);
freopen("conout$","w",stderr);


#3 SiS-Shadowman   Members   -  Reputation: 359

Like
0Likes
Like

Posted 28 April 2009 - 11:45 PM

If you want to write stuff to the debug window of VS, you need to use the OutputDebugString function.

#4 rozz666   Members   -  Reputation: 600

Like
0Likes
Like

Posted 29 April 2009 - 12:08 AM

Try this:

std::cout << "\r\n Test the water!" << std::flush;



#5 Prefect   Members   -  Reputation: 373

Like
0Likes
Like

Posted 29 April 2009 - 10:04 AM

To expand on rozz' post: Most likely, your strings are caught in some stream buffer which is not being flushed at exit of your program.

Using std::flush flushes these buffers explicitly. Outputting std::endl flushes the buffers implicitly (and creates a linebreak).

Finally, note that "\r\n" is non-standard. If your streams are opened in text mode (which they should be), a single "\n" will do the job, though it is more idiomatic to use std::endl.

#6 JohnsonGPS   Members   -  Reputation: 109

Like
0Likes
Like

Posted 29 April 2009 - 10:07 AM

Thank you fopr your help. Here aqre some updates.

1) Method 1 works.
AllocConsole();
freopen("conin$","r",stdin);
freopen("conout$","w",stdout);
freopen("conout$","w",stderr);
printf ("\r\n Test");


2) Method 2 passed the compilation, but still no output
OutputDebugString (_T("\r\nTest"));



#7 JohnsonGPS   Members   -  Reputation: 109

Like
0Likes
Like

Posted 29 April 2009 - 10:14 AM

Quote:
Original post by Prefect
To expand on rozz' post: Most likely, your strings are caught in some stream buffer which is not being flushed at exit of your program.

Using std::flush flushes these buffers explicitly. Outputting std::endl flushes the buffers implicitly (and creates a linebreak).

Finally, note that "\r\n" is non-standard. If your streams are opened in text mode (which they should be), a single "\n" will do the job, though it is more idiomatic to use std::endl.


I tried both std::endl and std::flush, and one of them could make the outputs visible. Thanks anyway.

#8 visitor   Members   -  Reputation: 643

Like
0Likes
Like

Posted 29 April 2009 - 10:35 AM

I wonder though, why would the program exit so abruptly that flushing is necessary. If it is for debugging, then you could use std::cerr which should flush each output automatically.

Quote:

though it is more idiomatic to use std::endl


Is it, seeing that is doing more than is usually needed (flushing the stream) and is more to type?

#9 Nitage   Members   -  Reputation: 810

Like
0Likes
Like

Posted 29 April 2009 - 10:50 AM

You need a debugger attched if you want to see output from OutputDebugString - did you start the program in debug mode from the IDE?

#10 juturnas   Members   -  Reputation: 136

Like
0Likes
Like

Posted 29 April 2009 - 10:56 AM

Quote:
Is it, seeing that is doing more than is usually needed (flushing the stream) and is more to type?


I would say more idiomatic yes, efficient no.

#11 Drew_Benton   Crossbones+   -  Reputation: 1713

Like
0Likes
Like

Posted 29 April 2009 - 12:30 PM

Quote:
I wrote a simple Windows Form application


You created a Windows Forms Application from the CLR tab. You should be creating a Win32 Console Application or Win32 Project under the Win32 tab.

I just tested using a CLR project and got the same results as you did with OutputDebugString failing to output. I don't know why that's the case, but it works under normal Win32 projects just fine. I would suggest you recreate your project as a Win32 Console project unless you are intentionally wanting to use a CLR styled project (which I would think you aren't after that at this point).

#12 theOcelot   Members   -  Reputation: 498

Like
0Likes
Like

Posted 29 April 2009 - 01:34 PM

Quote:
Original post by visitor
I wonder though, why would the program exit so abruptly that flushing is necessary. If it is for debugging, then you could use std::cerr which should flush each output automatically.


Seriously? Wish I'd known that about a year ago.



#13 adeyblue   Members   -  Reputation: 518

Like
0Likes
Like

Posted 29 April 2009 - 02:31 PM

Quote:
Original post by Drew_Benton
I just tested using a CLR project and got the same results as you did with OutputDebugString failing to output. I don't know why that's the case, but it works under normal Win32 projects just fine.


VS only attaches the managed debugger to /clr:pure projects by default. The native portion of the debugger needs to be attached for OutputDebugString to be effective. Switch the
Project Properties->Configuration Properties->Debugging->Debugger Type option to Mixed and it'll work.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS