用C编写的程序完成后如何防止控制台关闭?

发布于 2024-08-13 18:39:11 字数 364 浏览 3 评论 0原文

可能的重复:
防止控制台关闭的最佳实践是什么问题?

在 C 语言中程序完成后如何防止控制台关闭?当我尝试搜索它时,我发现了很多关于 C++ 和其他语言的内容,但没有任何关于 C 的内容。而且,即使对于 C++ 似乎也没有明确的答案。

那么有人可以让我知道在 C 程序运行完毕后保持控制台打开的最简单方法(不需要超级优雅)是什么?

Possible Duplicate:
What is the Best Practice for Combating the Console Closing Issue?

How do you keep the console from closing after the program is done in C? When I try to search for it I find lots of stuff about C++ and other languages, but nothing for C. Also, even for C++ there doesn't seem to be a definitive answer.

So could someone please let me know what the simplest way (doesn't need to be super elegant) way to keep the console open after a C program is done running?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

兲鉂ぱ嘚淚 2024-08-20 18:39:11

前面的答案都假设您想要调用控制台应用程序,然后实质上让它“运行”并等待用户输入终止。如果这是正确的假设,那么对 GMan 的答案+1。但是,如果您询问如何通过快捷方式、“开始”->“运行”或其他某种机制调用此控制台应用程序并保持 cmd 窗口打开,那么您将需要通过 cmd.exe 本身带有 /k 选项,如下所示:

cmd.exe /k "foo.exe"

这将启动 cmd 窗口,运行控制台应用程序,然后使 cmd 窗口保持打开状态。这将解决上面的@Thanatos。他是正确的,你应该让控制台应用程序关闭。同样,对我来说,不清楚你真正要求的最终目标是什么。

如果我做出了错误的假设,请随意给我-1。

The previous answers are all assuming that you want to invoke the console app and then essentially leave it "running" and waiting for user input to terminate. If this is the correct assumption, then +1 to GMan's answer. However, if you are asking how to invoke this console app from either a shortcut, Start->Run or some other mechanism and leave the cmd window open, then you will need to invoke it via cmd.exe itself with the /k option like so:

cmd.exe /k "foo.exe"

This will start a cmd window, run your console app, and then leave the cmd window open. This will address @Thanatos above. He's correct in that you should let the console app close. Again, for me it's unclear what you're really asking for what the end goal should be.

If I made the wrong assumption(s), feel free to -1 me.

天气好吗我好吗 2024-08-20 18:39:11
  • 从命令运行程序
    行,而不是执行它
    直接。

  • Visual C++ 中的 Ctrl+F5。

  • run the program from the command
    line, instead of executing it
    directly.

  • Ctrl+F5 in Visual C++.

木有鱼丸 2024-08-20 18:39:11

控制台应用程序旨在从控制台运行。如果这样做,运行后您将看到控制台窗口,并且可以轻松查看程序的输出。

您可以使用 getchar() 之类的方法来强制应用程序等待按键。

Console applications are meant to be run from the console. If you do that, after running you'll be left with your console window, and can easily view the output of your program.

You can use something like getchar() to force the application to wait for a key-press.

胡渣熟男 2024-08-20 18:39:11

让控制台关闭。

如果您在程序中禁止关闭控制台,则会使程序的自动化变得困难,或者会使程序的输入格式变得奇怪。

相反,首先修复运行程序的任何内容,不要首先关闭终端窗口。如果这是 MS Visual Studio,请尝试 F5(启动但不调试)。如果需要调试,请在程序末尾放置一个断点。否则,请打开命令提示符/终端并自行运行该程序。

Let the console close.

If you prohibit, in the program, the console from closing, it will make automation with your program difficult, or it will make the format of the program's input strange.

Instead, fix whatever's running the program in the first place, to not close the terminal window in the first place. If this is MS Visual Studio, try F5 (Start without debugging). If you need debugging, place a breakmark at the program's end. Otherwise, open a command prompt/terminal and run the program there yourself.

梦里梦着梦中梦 2024-08-20 18:39:11

1) 您的 IDE 在程序开始之前打开控制台。
2) 你的程序结束
3) IDE 关闭控制台

a) 只需告诉 IDE 不要关闭控制台...或者
b) 让你的程序不结束。

a) 不知道该怎么做。
b) 在用于终止程序的return 0;之前添加

printf("Press ENTER a few times to terminate the program");
fflush(stdout);
getchar(); getchar(); getchar(); getchar();
getchar(); getchar(); getchar(); getchar();
return 0;

1) Your IDE opens the console before the program begins.
2) your program ends
3) the IDE closes the console

a) Just tell the IDE to not close the console ... or
b) make your program not end.

a) No idea how to do it.
b) right before the return 0; used to terminate the program add

printf("Press ENTER a few times to terminate the program");
fflush(stdout);
getchar(); getchar(); getchar(); getchar();
getchar(); getchar(); getchar(); getchar();
return 0;
倒带 2024-08-20 18:39:11

您可以在程序末尾使用 getch() 。
另一种方法是调试程序并在程序结束之前放置断点。

You could use getch() at the end of your program.
Another way is to debug the program and place a break point before the end of the program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文