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

11

我尝试在vs code中调试一些代码。一切都正常,但是当我在控制台中输入一些内容时,没有任何反应。

我的代码:

#include <stdio.h>

#define SIZE 20

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

    puts(arr);

    return 0;
}

你可以看到,我使用fgets()从控制台读取字符串,但当调试到该函数时,一切都停止了,我无法输入任何内容。

enter image description here

只有一个黑窗口。

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

#include <stdio.h>

#define SIZE 20

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

    puts(arr);

    return 0;
}

一切正常。

enter image description here

问题出在哪里?或许调试控制台无法读取任何内容?我如何使用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"
        }
    ]
}

和 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"
}

问题似乎是将输入导入标准输入。不确定这个链接是否有帮助:https://github.com/microsoft/vscode-go/issues/219 - Cyclonecode
@Cyclonecode 谢谢,但是它没有帮助到我,我已经找到了另一个答案。 - senoron
1个回答

21

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

"args": ["<", "/path/to/your/stdin/file"]

2
谢谢。我也在 "args": ["<", "${workspaceFolder}/myfile"] 中使用 ${workspaceFolder} - ikhvjs
1
如果您使用 mkfifo 技术,我很想了解一下您的经验。我认为可以通过交互式输入进行调试,例如 echo interactive input >> namedpipe,但我还没有尝试过。 - Matt Faus
相关文档:https://code.visualstudio.com/docs/editor/debugging#_redirect-inputoutput-tofrom-the-debug-target - starball

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接