简体   繁体   中英

Holding cmd.exe open on Vista

I'm writing C++ console programs. After compilation, when I run the program from my file browser, cmd.exe automatically closes such that I can't see my programs output.

The only way to work around this I've found is to run the program from inside cmd.exe

Is there anyway to keep cmd.exe open after a program finishes running?

Is there a setting I can change somewhere? I don't want to run a batch script with cmd.exe /K

Thanks!

[Edit] Don't know if this matters, but I'm on Vista x64

启动cmd.exe时,可以使用/ K开关设置快捷方式,使其在运行给定命令后不会终止:

 cmd.exe /K YourProgram.exe

让您的应用程序在退出之前要求按键 - 这是最简单的修复!

I've always been a fan of just creating a batch file that calls you're program and then calls pause

Prog.exe Pause

This will give a nice "Press any key to continue..." prompt, it's simple and doesn't require modification of program.

As the last line of your main() function, you can add this line:

system("PAUSE");

Also, make sure to #include <stdlib.h> to declare the system() function. This will make the console pause. However, if your program is run from inside cmd.exe , this will still pause, which may be undesirable.

I know you asked for how to do it via a file browsers, but in case other people are interested in the same problem but through visual studio:

It's best to set a breakpoint right before your program ends.

Then you can deploy your exe and you can be sure that you won't forget to remove the asking for input. It's also better then asking for input because it takes a lot of time to comment out and back in the asking for input.

I think it is best not to ask for input and to instead start your program from a launched command prompt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM