簡體   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