在 Vista 上保持 cmd.exe 打开
我正在编写 C++ 控制台程序。 编译后,当我从文件浏览器运行程序时,cmd.exe 自动关闭,因此我看不到程序输出。
我发现解决这个问题的唯一方法是从 cmd.exe 内部运行程序
程序完成运行后是否有办法保持 cmd.exe 打开?
我可以在某处更改设置吗? 我不想使用 cmd.exe /K 运行批处理脚本
谢谢!
[编辑] 不知道这是否重要,但我使用的是 Vista x64
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在启动 cmd.exe 时使用 /K 开关设置快捷方式,以使其在运行给定命令后不会终止:
You can setup a shortcut with the /K switch when launching cmd.exe to have it not terminate after running a given command:
让您的应用程序在退出前请求按键 - 这是最简单的解决方法!
Have your application ask for a keypress before exiting - that's the easiest fix!
我一直热衷于创建一个批处理文件,调用您的程序,然后调用暂停
Prog.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.
作为
main()
函数的最后一行,您可以添加以下行:此外,请确保
#include
声明system() 函数。 这将使控制台暂停。 但是,如果您的程序是从
cmd.exe
内部运行的,则该过程仍会暂停,这可能是不可取的。As the last line of your
main()
function, you can add this line:Also, make sure to
#include <stdlib.h>
to declare thesystem()
function. This will make the console pause. However, if your program is run from insidecmd.exe
, this will still pause, which may be undesirable.我知道您询问如何通过文件浏览器来完成此操作,但如果其他人对同一问题感兴趣但通过 Visual Studio:
最好在程序结束之前设置一个断点。
然后您可以部署您的 exe,并且可以确保您不会忘记删除要求输入的内容。 这也比请求输入更好,因为注释掉并返回请求输入需要花费大量时间。
我认为最好不要要求输入,而是从启动的命令提示符启动程序。
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.