visual-studio 在 Visual Studio (C) 中保持控制台窗口打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2083702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:25:08  来源:igfitidea点击:

Keeping Console Window Open in Visual Studio (C)

visual-studio

提问by Crystal

I usually use XCode but was having a problem opening a file with this code:

我通常使用 XCode,但在使用以下代码打开文件时遇到问题:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

int main(void )
{
   printf("Hello");
   FILE *filePtr;

   filePtr = fopen( "test.txt", "r" );
   if (filePtr == NULL)
   {
      fprintf(stderr, "Can't open \"test\"\n");
      exit(EXIT_FAILURE);
   }
   else
   {
      int x;

      printf("File open successful\n");
      /* read one character at a time until EOF is reached */
      while ((x = fgetc(filePtr)) != EOF)
      {
         //printf("%c", x);
         fprintf(stderr, "%x\n",x);
      }
   }
   fclose(filePtr);
   system("pause");

   return EXIT_FAILURE;
}

The console window closes so fast and at the bottom bar of VS it says: "'C_test.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll' The program '[1116] C_test.exe: Native' has exited with code 1 (0x1)." What does that mean?

控制台窗口关闭得如此之快,在 VS 的底部栏上显示:“'C_test.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f86msdvcr71 program '[1116] C_test.exe: Native' 已退出,代码为 1 (0x1)。” 这意味着什么?

Also, can anyone point me to good VS starting points / tutorials?

另外,任何人都可以向我指出好的 VS 起点/教程吗?

回答by Simon Buchan

I generally leave a breakpoint on the closing brace of main, so that the output of my window is visible while debugging, but Visual Studio will keep the console window open if you start the program without debugging (Ctrl+F5). Alternatively, you could simply ask for input, @MadcapLaugher's fgetc(STDIN);is probably your best bet - though I would add a prompt: "Press any key to continue... "

我通常会在 main 的右大括号上留一个断点,以便在调试时可以看到我的窗口的输出,但是如果您在不调试的情况下启动程序(Ctrl+F5),Visual Studio 将保持控制台窗口打开。或者,您可以简单地要求输入,@MadcapLaugherfgetc(STDIN);可能是您最好的选择 - 尽管我会添加一个提示:"Press any key to continue... "

回答by MadcapLaugher

The reason you can't see it is because there is no possibility for your program to pause during execution. In Visual Studio the typical behavior is to close the console window the second the program has completed its execution.

你看不到它的原因是你的程序不可能在执行过程中暂停。在 Visual Studio 中,典型的行为是在程序完成执行后关闭控制台窗口。

The bottom bar is telling you that the program complete and what the return value was (1 in this case).

底部栏告诉您程序已完成以及返回值是什么(在本例中为 1)。

What I would also do is add code right before the exit point of the program with #ifdefs:

我还会做的是在程序退出点之前使用#ifdefs 添加代码:

#ifdef VS_DEBUG
fgetc(STDIN);
#endif

Now your program will pause when it's done and wait for a keypress then close the window.

现在您的程序将在完成后暂停并等待按键然后关闭窗口。

I'm sure there is also a way in the project settings to prevent the closing, I've never looked myself.

我确定项目设置中也有防止关闭的方法,我从来没有看过自己。

回答by MD Rakibuz Sultan

 #include<stdio.h>
 int main() 
{

    printf("Hello World from visual Studio \n");

    //to prevent console window temination 
    system("pause");

    return 0;
}