在调试控制台 VScode 中使用输入(stdin)

sen*_*ron 12 c debugging stdin fgets visual-studio-code

我尝试在 VS Code 中调试一些代码。一切正常,但是当我尝试在控制台中输入某些内容时,什么也没有发生。

我的代码:

#include <stdio.h>

#define SIZE 20

int main()
{
    char arr[SIZE];
    
    fgets(arr, SIZE, stdin);

    puts(arr);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我过去常常fgets()从控制台读取字符串,但是当调试到达函数时,一切都会停止,我无法输入任何内容。

在此输入图像描述

只是黑窗。

但是,当我使用 时gets(),就像这个例子一样:

#include <stdio.h>

#define SIZE 20

int main()
{
    char arr[SIZE];
    
    gets(arr);

    puts(arr);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

一切正常。

在此输入图像描述

哪里有问题?也许调试控制台无法读取任何内容?我怎样才能使用输入任何内容fgets()

这是我的 launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "externalConsole": true,
            "environment": [],
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Users\\Svyatoslav\\gcc\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc.exe build active file"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

和tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\Users\\Svyatoslav\\gcc\\bin\\gcc.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\Users\\Svyatoslav\\gcc\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Generated task by Debugger"
        }
    ],
    "version": "2.0.0"
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*aus 25

在 launch.json 中,您可以通过以下方式指定连接到 stdin 的文件:

"args": ["<", "/path/to/your/stdin/file"]
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。我还在 `"args": ["&lt;", "${workspaceFolder}/myfile"]` 中使用 `${workspaceFolder}` (3认同)