防止控制台窗口关闭Visual Studio C/ c++控制台应用程序。

时间:2022-05-11 00:38:11

This is a probably an embarasing question as no doubt the answer is blindingly obvious.

这可能是一个令人尴尬的问题,毫无疑问,答案是显而易见的。

I've used Visual Studio for years, but this is the first time I've done any 'Console Application' development.

我已经使用Visual Studio很多年了,但这是我第一次做任何“控制台应用程序”开发。

When I run my application the console window pops up, the program output appears and then the window closes as the application exits.

当我运行我的应用程序时,控制台窗口会弹出,程序输出出现,然后当应用程序退出时窗口关闭。

Is there a way to either keep it open until I have checked the output, or view the results after the window has closed?

是否有一种方法可以让它保持打开,直到我检查了输出,或者在窗口关闭后查看结果?

16 个解决方案

#1


361  

If you run without debugging (Ctrl+F5) then by default it prompts your to press return to close the window. If you want to use the debugger, you should put a breakpoint on the last line.

如果您在没有调试的情况下运行(Ctrl+F5),那么默认情况下,它会提示您按return键关闭窗口。如果您想要使用调试器,您应该在最后一行上设置一个断点。

#2


143  

Right click on your project

右键单击项目。

Properties>Configuration Properties> Linker> System

属性- >配置属性>链接器>系统

select Console (/SUBSYSTEM:CONSOLE) in SubSystem option.

在子系统选项中选择控制台(/子系统:控制台)。

Now try it...it should work

现在试一试…它应该工作

#3


39  

Here is a way for C/C++:

这是C/ c++的一种方法:

#include <stdlib.h>

#ifdef _WIN32
    #define WINPAUSE system("pause")
#endif

Put this at the top of your program, and IF it is on a Windows system (#ifdef _WIN32), then it will create a macro called WINPAUSE. Whenever you want your program to pause, call WINPAUSE; and it will pause the program, using the DOS command. For other systems like Unix/Linux, the console should not quit on program exit anyway.

把这个放在程序的顶部,如果它在Windows系统上(#ifdef _WIN32),那么它将创建一个名为WINPAUSE的宏。每当你想让程序暂停时,调用WINPAUSE;它将使用DOS命令暂停程序。对于Unix/Linux等其他系统,控制台不应该退出程序退出。

#4


24  

Goto Debug Menu->Press StartWithoutDebugging

转到调试菜单- >按StartWithoutDebugging

#5


18  

If you're using .NET, put Console.ReadLine() before the end of the program.

如果您使用。net,则在程序结束前放置Console.ReadLine()。

It will wait for <ENTER>.

它将等待

#6


11  

try to call getchar() right before main() returns.

尝试在main()返回之前调用getchar()。

#7


9  

(/SUBSYSTEM:CONSOLE) did not worked for my vs2013 (I already had it).

(/子系统:控制台)没有为我的vs2013工作(我已经有了)。

"run without debugging" is not an options, since I do not want to switch between debugging and seeing output.

“运行不调试”不是一个选项,因为我不想在调试和看到输出之间切换。

I ended with

我结束了

int main() {
  ...
#if _DEBUG
  LOG_INFO("end, press key to close");
  getchar();
#endif // _DEBUG
  return 0;
}

Solution used in qtcreator pre 2.6. Now while qt is growing, vs is going other way. As I remember, in vs2008 we did not need such tricks.

在qtcreator前2.6中使用的解决方案。在qt增长的同时,vs正走向另一个方向。我记得,在2008年的vs2008,我们不需要这样的把戏。

#8


7  

Here's a solution that (1) doesn't require any code changes or breakpoints, and (2) pauses after program termination so that you can see everything that was printed. It will pause after either F5 or Ctrl+F5. The major downside is that on VS2013 Express (as tested), it doesn't load symbols, so debugging is very restricted.

这里有一个解决方案(1)不需要任何代码更改或断点,并且(2)在程序终止后暂停,这样您就可以看到所有打印的内容。它会在F5或Ctrl+F5之后暂停。主要的缺点是在VS2013 Express上(经过测试),它没有加载符号,所以调试非常受限制。

  1. Create a batch file. I called mine runthenpause.bat, with the following contents:

    创建一个批处理文件。我叫runthenpause。bat,有以下内容:

    %1 %2 %3 %4 %5 %6 %7 %8 %9
    pause
    

    The first line will run whatever command you provide and up to eight arguments. The second line will... pause.

    第一行将运行您提供的任何命令和多达8个参数。将第二行……暂停。

  2. Open the project properties | Configuration properties | Debugging.

    打开项目属性|配置属性|调试。

  3. Change "Command Arguments" to $(TargetPath) (or whatever is in "Command").
  4. 将“命令参数”改为$(TargetPath)(或任何“命令”)。
  5. Change "Command" to the full path to runthenpause.bat.
  6. 将“命令”更改为runthenpause.bat的完整路径。
  7. Hit OK.
  8. 点击确定。

Now, when you run, runthenpause.bat will launch your application, and after your application has terminated, will pause for you to see the console output.

现在,当你运行时,runthenpause。bat将启动您的应用程序,在您的应用程序终止之后,将暂停您查看控制台输出。

I will post an update if I figure out how to get the symbols loaded. I tried /Z7 per this but without success.

我将发布一个更新,如果我弄清楚如何得到这些符号。我试了一下,但没有成功。

#9


2  

You could run your executable from a command prompt. This way you could see all the output. Or, you could do something like this:

可以从命令提示符运行可执行文件。这样你就能看到所有的输出。或者,你可以这样做:

int a = 0;
scanf("%d",&a);

return YOUR_MAIN_CODE;

and this way the window would not close until you enter data for the a variable.

这样窗口就不会关闭,直到你输入一个变量的数据。

#10


2  

add “| pause” in command arguments box under debugging section at project properties.

在项目属性调试部分的命令参数框中添加“|暂停”。

#11


2  

Just press CNTRL + F5 to open it in an external command line window (Visual Studio does not have control over it).

只需按CNTRL + F5在外部命令行窗口中打开它(Visual Studio无法控制它)。

If this doesn't work then add the following to the end of your code:

如果这个方法不起作用,那么在代码的末尾添加以下内容:

Console.WriteLine("Press any key to exit...");
Console.ReadKey();

This wait for you to press a key to close the terminal window once the code has reached the end.

当代码到达末尾时,等待您按下一个键关闭终端窗口。

If you want to do this in multiple places, put the above code in a method (e.g. private void Pause()) and call Pause() whenever a program reaches a possible end.

如果您想在多个地方执行此操作,请将上述代码置于方法中(例如,私有void Pause()),并在程序到达可能的一端时调用Pause()。

#12


2  

just put as your last line of code:

把你的最后一行代码写出来:

system("pause");

#13


1  

Either use:

要么使用:

  1. cin.get();
  2. cin.get();

or

  1. system("pause");
  2. 系统(“暂停”);

Make sure to make either of them at the end of main() function and before the return statement.

确保在main()函数的末尾和return语句之前将它们都做了。

#14


0  

A somewhat better solution:

一个更好的解决方案:

atexit([] { system("PAUSE"); });

at the beginning of your program.

在你的程序开始的时候。

Pros:

优点:

  • can use std::exit()
  • 可以使用std::退出()
  • can have multiple returns from main
  • 可以从主服务器获得多个返回值吗?
  • you can run your program under the debugger
  • 您可以在调试器下运行程序。
  • IDE independent (+ OS independent if you use the cin.sync(); cin.ignore(); trick instead of system("pause");)
  • 独立的IDE(如果使用cin.sync(),则独立操作系统;cin.ignore();技巧,而不是系统(“暂停”),)

Cons:

缺点:

  • have to modify code
  • 必须修改代码
  • won't pause on std::terminate()
  • 不会暂停std::终止()
  • will still happen in your program outside of the IDE/debugger session; you can prevent this under Windows using:
  • 在IDE/调试器会话之外的程序中仍然会发生;你可以在Windows下使用:

extern "C" int __stdcall IsDebuggerPresent(void);
int main(int argc, char** argv) {
    if (IsDebuggerPresent())
        atexit([] {system("PAUSE"); });
    ...
}

#15


-1  

Visual Studio 2015, with imports. Because I hate when code examples don't give the needed imports.

Visual Studio 2015,导入。因为我讨厌代码示例不提供所需的导入。

#include <iostream>;

int main()
{
    getchar();
    return 0;
}

#16


-3  

Use console.readline. Yours is writing the line but not reading it.

使用console.readline。你写的是行,而不是读。

#1


361  

If you run without debugging (Ctrl+F5) then by default it prompts your to press return to close the window. If you want to use the debugger, you should put a breakpoint on the last line.

如果您在没有调试的情况下运行(Ctrl+F5),那么默认情况下,它会提示您按return键关闭窗口。如果您想要使用调试器,您应该在最后一行上设置一个断点。

#2


143  

Right click on your project

右键单击项目。

Properties>Configuration Properties> Linker> System

属性- >配置属性>链接器>系统

select Console (/SUBSYSTEM:CONSOLE) in SubSystem option.

在子系统选项中选择控制台(/子系统:控制台)。

Now try it...it should work

现在试一试…它应该工作

#3


39  

Here is a way for C/C++:

这是C/ c++的一种方法:

#include <stdlib.h>

#ifdef _WIN32
    #define WINPAUSE system("pause")
#endif

Put this at the top of your program, and IF it is on a Windows system (#ifdef _WIN32), then it will create a macro called WINPAUSE. Whenever you want your program to pause, call WINPAUSE; and it will pause the program, using the DOS command. For other systems like Unix/Linux, the console should not quit on program exit anyway.

把这个放在程序的顶部,如果它在Windows系统上(#ifdef _WIN32),那么它将创建一个名为WINPAUSE的宏。每当你想让程序暂停时,调用WINPAUSE;它将使用DOS命令暂停程序。对于Unix/Linux等其他系统,控制台不应该退出程序退出。

#4


24  

Goto Debug Menu->Press StartWithoutDebugging

转到调试菜单- >按StartWithoutDebugging

#5


18  

If you're using .NET, put Console.ReadLine() before the end of the program.

如果您使用。net,则在程序结束前放置Console.ReadLine()。

It will wait for <ENTER>.

它将等待

#6


11  

try to call getchar() right before main() returns.

尝试在main()返回之前调用getchar()。

#7


9  

(/SUBSYSTEM:CONSOLE) did not worked for my vs2013 (I already had it).

(/子系统:控制台)没有为我的vs2013工作(我已经有了)。

"run without debugging" is not an options, since I do not want to switch between debugging and seeing output.

“运行不调试”不是一个选项,因为我不想在调试和看到输出之间切换。

I ended with

我结束了

int main() {
  ...
#if _DEBUG
  LOG_INFO("end, press key to close");
  getchar();
#endif // _DEBUG
  return 0;
}

Solution used in qtcreator pre 2.6. Now while qt is growing, vs is going other way. As I remember, in vs2008 we did not need such tricks.

在qtcreator前2.6中使用的解决方案。在qt增长的同时,vs正走向另一个方向。我记得,在2008年的vs2008,我们不需要这样的把戏。

#8


7  

Here's a solution that (1) doesn't require any code changes or breakpoints, and (2) pauses after program termination so that you can see everything that was printed. It will pause after either F5 or Ctrl+F5. The major downside is that on VS2013 Express (as tested), it doesn't load symbols, so debugging is very restricted.

这里有一个解决方案(1)不需要任何代码更改或断点,并且(2)在程序终止后暂停,这样您就可以看到所有打印的内容。它会在F5或Ctrl+F5之后暂停。主要的缺点是在VS2013 Express上(经过测试),它没有加载符号,所以调试非常受限制。

  1. Create a batch file. I called mine runthenpause.bat, with the following contents:

    创建一个批处理文件。我叫runthenpause。bat,有以下内容:

    %1 %2 %3 %4 %5 %6 %7 %8 %9
    pause
    

    The first line will run whatever command you provide and up to eight arguments. The second line will... pause.

    第一行将运行您提供的任何命令和多达8个参数。将第二行……暂停。

  2. Open the project properties | Configuration properties | Debugging.

    打开项目属性|配置属性|调试。

  3. Change "Command Arguments" to $(TargetPath) (or whatever is in "Command").
  4. 将“命令参数”改为$(TargetPath)(或任何“命令”)。
  5. Change "Command" to the full path to runthenpause.bat.
  6. 将“命令”更改为runthenpause.bat的完整路径。
  7. Hit OK.
  8. 点击确定。

Now, when you run, runthenpause.bat will launch your application, and after your application has terminated, will pause for you to see the console output.

现在,当你运行时,runthenpause。bat将启动您的应用程序,在您的应用程序终止之后,将暂停您查看控制台输出。

I will post an update if I figure out how to get the symbols loaded. I tried /Z7 per this but without success.

我将发布一个更新,如果我弄清楚如何得到这些符号。我试了一下,但没有成功。

#9


2  

You could run your executable from a command prompt. This way you could see all the output. Or, you could do something like this:

可以从命令提示符运行可执行文件。这样你就能看到所有的输出。或者,你可以这样做:

int a = 0;
scanf("%d",&a);

return YOUR_MAIN_CODE;

and this way the window would not close until you enter data for the a variable.

这样窗口就不会关闭,直到你输入一个变量的数据。

#10


2  

add “| pause” in command arguments box under debugging section at project properties.

在项目属性调试部分的命令参数框中添加“|暂停”。

#11


2  

Just press CNTRL + F5 to open it in an external command line window (Visual Studio does not have control over it).

只需按CNTRL + F5在外部命令行窗口中打开它(Visual Studio无法控制它)。

If this doesn't work then add the following to the end of your code:

如果这个方法不起作用,那么在代码的末尾添加以下内容:

Console.WriteLine("Press any key to exit...");
Console.ReadKey();

This wait for you to press a key to close the terminal window once the code has reached the end.

当代码到达末尾时,等待您按下一个键关闭终端窗口。

If you want to do this in multiple places, put the above code in a method (e.g. private void Pause()) and call Pause() whenever a program reaches a possible end.

如果您想在多个地方执行此操作,请将上述代码置于方法中(例如,私有void Pause()),并在程序到达可能的一端时调用Pause()。

#12


2  

just put as your last line of code:

把你的最后一行代码写出来:

system("pause");

#13


1  

Either use:

要么使用:

  1. cin.get();
  2. cin.get();

or

  1. system("pause");
  2. 系统(“暂停”);

Make sure to make either of them at the end of main() function and before the return statement.

确保在main()函数的末尾和return语句之前将它们都做了。

#14


0  

A somewhat better solution:

一个更好的解决方案:

atexit([] { system("PAUSE"); });

at the beginning of your program.

在你的程序开始的时候。

Pros:

优点:

  • can use std::exit()
  • 可以使用std::退出()
  • can have multiple returns from main
  • 可以从主服务器获得多个返回值吗?
  • you can run your program under the debugger
  • 您可以在调试器下运行程序。
  • IDE independent (+ OS independent if you use the cin.sync(); cin.ignore(); trick instead of system("pause");)
  • 独立的IDE(如果使用cin.sync(),则独立操作系统;cin.ignore();技巧,而不是系统(“暂停”),)

Cons:

缺点:

  • have to modify code
  • 必须修改代码
  • won't pause on std::terminate()
  • 不会暂停std::终止()
  • will still happen in your program outside of the IDE/debugger session; you can prevent this under Windows using:
  • 在IDE/调试器会话之外的程序中仍然会发生;你可以在Windows下使用:

extern "C" int __stdcall IsDebuggerPresent(void);
int main(int argc, char** argv) {
    if (IsDebuggerPresent())
        atexit([] {system("PAUSE"); });
    ...
}

#15


-1  

Visual Studio 2015, with imports. Because I hate when code examples don't give the needed imports.

Visual Studio 2015,导入。因为我讨厌代码示例不提供所需的导入。

#include <iostream>;

int main()
{
    getchar();
    return 0;
}

#16


-3  

Use console.readline. Yours is writing the line but not reading it.

使用console.readline。你写的是行,而不是读。