【问题标题】:Use input (stdin) in debug console VScode在调试控制台 VScode 中使用输入(stdin)
【发布时间】:2020-11-11 12:09:08
【问题描述】:

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

我的代码:

#include <stdio.h>

#define SIZE 20

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

    puts(arr);

    return 0;
}

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

只是黑窗。

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

#include <stdio.h>

#define SIZE 20

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

    puts(arr);

    return 0;
}

一切正常。

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

【问题讨论】:

标签: c debugging visual-studio-code stdin fgets


【解决方案1】:

在launch.json中,你可以指定一个文件连接到stdin via:

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

【讨论】:

  • 谢谢。我也在"args": ["&lt;", "${workspaceFolder}/myfile"]中使用${workspaceFolder}
【解决方案2】:

好的,我找到了我的问题的答案。 解决方案是使用来自 mingw-w64 的 GCC 编译器 (gcc) 和 GDB 调试器。

这是帮助我的帖子:press me

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 2019-10-04
    • 2021-02-17
    • 2017-06-02
    • 2018-12-18
    • 2020-07-31
    相关资源
    最近更新 更多