简体   繁体   English

c控制台程序

[英]c console programs

I have a *.exe console file. 我有一个* .exe控制台文件。 I enter my inputs, and everything is great. 我输入了输入,一切都很好。

but when I enter the last input, the command window closes (because the program has ended) before I can read the last output. 但是当我输入最后一个输入时,在我可以读取最后一个输出之前,命令窗口将关闭(因为程序已结束)。

is there a way to run that *.exe file and force it to stay open after the program ends? 有没有办法运行该* .exe文件,并在程序结束后强制其保持打开状态?

note: this is not my program. 注意:这不是我的程序。 I can't edit the source code, so I'm not looking for answers like "add while(1) or scanf at the end". 我无法编辑源代码,因此我没有在寻找“添加while(1)或末尾scanf”之类的答案。

Thanks ahead. 谢谢你

Just open a command prompt and run it, the way it's meant to be used. 只需打开命令提示符并按照其使用方式运行它即可。

Start -> Run -> cmd.exe 开始->运行-> cmd.exe

or Win+R -> cmd 或Win + R-> cmd

从命令提示符下运行程序(Start-Run-cmd.exe)

Make a batch file (*.bat), with the command you want to execute, followed by pause: 使用要执行的命令制作一个批处理文件(* .bat),然后暂停:

myconsoleapplication.exe
pause

Save it, and run. 保存并运行。 The command window wil wait for enter to be pressed before closing. 在关闭之前,命令窗口将等待按下Enter键。

Run it from the console, or in a batch file. 从控制台或批处理文件中运行它。

Start >> Run >> cmd.exe 开始>>运行>> cmd.exe

Then the console window is already open. 然后,控制台窗口已经打开。

This should work: 这应该工作:

system("pause");

At the top of your program, include stdlib.h: 在程序的顶部,包括stdlib.h:

#include <stdlib.h>

You can open a command prompt window and navigate (the cd command) to the directory containing the .exe file. 您可以打开命令提示符窗口,然后导航( cd命令)到包含.exe文件的目录。 Then, run the program by typing its name. 然后,通过输入程序名称来运行该程序。 The window will not close after the program finishes running. 程序完成运行后,该窗口不会关闭。

You could execute this program from one of your own, redirecting stdio and adding your own pause after it terminates. 您可以从自己的一个执行该程序,重定向stdio并在终止后添加自己的暂停。 I've done this with .NET but assume that it can be done through other means. 我已经使用.NET完成了此操作,但假设可以通过其他方式完成。 Of course a batch file, as mentioned already, is wicked simpler. 当然,如前所述,批处理文件更为简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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