views:

1251

answers:

5

Is there a way short of writing a seperate batch file or adding a system call to pause or getch or a breakpoint right before the end of the main function to keep a command window open after a command line application has finished running?

Put differently, is there a way in the project properties to run another command after running the target path? If my program is "foo.exe", something equivalent to a batch file containing

@foo
@pause

Edit: added "or a getch or a breakpoint"

A: 

If you run a command line application from an existing command line window, then it won't be closed when it finishes. This is the expected usual case for a command line program.

If you run a command line application by double-clicking on it in Explorer, a new command window is opened until the program completes, then it is automatically closed. I don't know of a way to ask Windows to keep that command window open.

Greg Hewgill
There is a way: make a .pif (not .lnk) which points to your program and uncheck "close on exit". The disadvantage is the overhead of an instance of NTVDM.
Hugh Allen
A: 

In C++

#include <conio.h>

// .. Your code

int main()
{
  // More of your code

  // Tell the user to press a key 
  getch() // Get one character from the user (i.e a keypress)
  return 0;
}

If you are in a batch file, the command "pause" outputs "Press any key to continue" and waits for a keypress.

Tom Leys
+2  A: 

If running in the debugger (going by the title of your question), I just put a breakpoint at the closing brace in the main method.

tvanfosson
A: 

If one has access to source code,

1.) Will adding a getch() added just before main ends, not puase the application executuion, while displaying the console applications console window as it was? Basically add any code which waits on a keyboard input

-AD

goldenmean
+2  A: 

As you may know, at least in the C# project system, pressing Ctrl-F5 will add a "press any key to continue" to the end. But this doesn't run under the debugger, so I endorse the prior answer that said 'put a breakpoint at the end of main'.

Brian