Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Visual Studio代码中调试C代码时,我的输出在哪里?_C_Windows_Visual Studio Code_Vscode Debugger - Fatal编程技术网

在Visual Studio代码中调试C代码时,我的输出在哪里?

在Visual Studio代码中调试C代码时,我的输出在哪里?,c,windows,visual-studio-code,vscode-debugger,C,Windows,Visual Studio Code,Vscode Debugger,我有一个简单的c程序,在屏幕上打印一些东西。 当我调试程序时,我可以看到调试控制台,但是只要我使用fgets,我就看不到任何输出。使用VS代码进行调试时,我的程序运行在哪里 如果我只是运行编译的.exe,所有内容都会按预期打印 #include <stdio.h> #include <stdlib.h> int main() { printf("Hello World!\n"); printf("Enter your name\n"); char name

我有一个简单的c程序,在屏幕上打印一些东西。 当我调试程序时,我可以看到调试控制台,但是只要我使用fgets,我就看不到任何输出。使用VS代码进行调试时,我的程序运行在哪里

如果我只是运行编译的.exe,所有内容都会按预期打印

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

int main() {
  printf("Hello World!\n");
  printf("Enter your name\n");

  char name[100];
  // fgets(name, sizeof(name), stdin); // as soon as I uncomment this, no output is in the output console
  printf("You name %s", name);

  return 0;
}

一个解决方案可以解决你的问题

如何从VSCode配置
tasks.json
launch.json
您不需要为此执行任何类型的编码,只需遵循以下步骤:

  • 删除
    .vscode
    文件夹中的
    tasks.json
    launch.json
  • 再次按下
    F5
    (调试快捷方式),关注该C程序文件,您将看到如下内容:
  • 选择您的编译器,它是
    GCC
    (因为您试图调试一个C程序,并确保编译器已安装到您的系统中)
  • 系统将提示您选择一个配置(假设选择了选项:GCC),您将获得VSCode自动创建的
    launch.json
    ,如下所示:
  • 注意:记住
    preLaunchTask
    配置(位于配置的最底部)

  • 再次按下
    F5
    (这次是创建
    tasks.json
    )后,您将看到如下所示,只需选择配置任务即可:
  • 现在,您将被重定向到
    tasks.json
    ,将
    标签
    编辑为您在§4中选择的名称(请记住
    名称
    )。换句话说,启动的
    preLaunchTask
    和任务的
    标签应该相同。流程如下所示:
  • 现在,您可以进行成功的调试。一个有效的例子:


    只需在launch.json中将
    externalConsole:true
    添加到您的配置中即可

    例如:

    launch.json

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "(Windows) Launch",
          "type": "cppvsdbg",
          "request": "launch",
          "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${workspaceFolder}",
          "environment": [],
          "externalConsole": true,
          "preLaunchTask": "cl.exe build active file",
        }
      ]
    }
    
    { 
      "version": "2.0.0",
      "tasks": [
        {
          "type": "shell",
          "label": "cl.exe build active file",
          "command": "cl.exe",
          "args": [
            "/Zi",
            "/EHsc",
            "/Fe:",
            "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "${file}"
          ],
          "problemMatcher": ["$msCompile"],
          "group": {
            "kind": "build",
            "isDefault": true
          },
        }
      ]
    }
    
    tasks.json

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "(Windows) Launch",
          "type": "cppvsdbg",
          "request": "launch",
          "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${workspaceFolder}",
          "environment": [],
          "externalConsole": true,
          "preLaunchTask": "cl.exe build active file",
        }
      ]
    }
    
    { 
      "version": "2.0.0",
      "tasks": [
        {
          "type": "shell",
          "label": "cl.exe build active file",
          "command": "cl.exe",
          "args": [
            "/Zi",
            "/EHsc",
            "/Fe:",
            "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "${file}"
          ],
          "problemMatcher": ["$msCompile"],
          "group": {
            "kind": "build",
            "isDefault": true
          },
        }
      ]
    }
    

    是否添加了断点在代码中?如果我添加一个断点,它会被命中,我可以逐步遍历代码,查看变量,但是标准的输入和输出不在调试窗格中,这让我们感觉到,因为它是只读窗口。请尝试重新配置
    tasks.json
    launch.json
    它们可能会修复您的错误。您可以发布整个tasks.json并启动吗。json?我没有使用gcc.exe,您提到的prelaunchtask根本不可见。那么您使用的是什么?@Liero you go。(zip仅包含任务和launch.json)。请不要忘记使用您自己的设置进行配置,否则如果只是复制粘贴而什么都不做,则可能无法使用。谢谢。但是,下次,请不要上载ZIP,而是在答案中包含所有相关详细信息。不要过度使用屏幕截图,因为无法从中复制文本。