繁体   English   中英

如何使用集成的 Visual Studio Code 终端从标准输入读取数据?

[英]How can I read from stdin using the integrated Visual Studio Code terminal?

我有以下 C 应用程序:

#include <stdio.h>
#include <string.h>
int main() {
    char name[10];
    printf("What is your name?\n");
    fflush(stdout);
    fscanf(stdin, "%s", name);
    printf("Your name is %s.\n", name);
    return 0;
}

我正在运行它:

  • 视觉工作室代码 1.48.2
  • C/C++(Visual Studio 代码扩展)0.29.0
  • Code Runner(Visual Studio Code 的扩展)0.11.0
  • MinGW(mingw32-base)2013072200

当我按 CTRL+F5 (实际上是Run without Debugging )时,我看到提示, What is your name? 在调试控制台中。 我尝试在调试控制台中输入我的名字,它给出了以下错误: Unable to perform this action because the process is running.

如何使用集成终端从stdin读取?

这是我的launch.json配置:

{
    "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}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc.exe build active file"
        }
    ]
}

通过在上述配置中设置"externalConsole": true,我能够使用外部控制台从stdin输入获取输入,但我正在寻找一种方法,如果可能的话,能够简单地将我的名字写在一个集成的 IDE 控制台选项卡,这样我就不需要启动外部控制台。

我不会说英语,所以如果我的写作很奇怪,我很抱歉。

我发布这个是因为我发生了类似的情况并且我能够解决它。

我用这个作为参考。 https://github.com/microsoft/vscode-cpptools/issues/5497#issuecomment-628878810

就我而言,集成终端不接受键盘输入。

解决方案是将编译器更改为此。 ( https://sourceforge.net/projects/mingw-w64/ )

你可能需要创建一个路径,然后重写tasks.json和launch.json

这里以我的 tasks.json 和 launch.json 为例。 请参考它。

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe アクティブなファイルのビルド",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "デバッガーによって生成されたタスク。"
        }
    ],
    "version": "2.0.0"
}
{
    // IntelliSense を使用して利用可能な属性を学べます。
    // 既存の属性の説明をホバーして表示します。
    // 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - アクティブ ファイルのビルドとデバッグ",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "gdb の再フォーマットを有効にする",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe アクティブなファイルのビルド"
        }
    ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM