简体   繁体   中英

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

I have the following C application:

#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;
}

I am running it with:

  • Visual Studio Code 1.48.2
  • C/C++ (Extension for Visual Studio Code) 0.29.0
  • Code Runner (Extension for Visual Studio Code) 0.11.0
  • MinGW (mingw32-base) 2013072200

When I hit CTRL+F5 (effectively, Run Without Debugging ), I see the prompt, What is your name? in the Debug Console. I try to type my name into the Debug Console, and it gives the following error: Unable to perform this action because the process is running.

How can I read from stdin using the integrated terminal?

Here is my launch.json configuration:

{
    "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"
        }
    ]
}

I was able to get it to take input from stdin using the external console by setting "externalConsole": true, in the above configuration, but I am looking for a way, if possible, to be able to simply write my name in one of the integrated IDE console tabs so that I don't need to launch an external console.

I'm not an English speaker, so I apologize if my writing is strange.

I'm posting this because a similar case occurred to me and I was able to solve it.

I used this as a reference. ( https://github.com/microsoft/vscode-cpptools/issues/5497#issuecomment-628878810 )

In my case, the integrated terminal was not accepting keyboard input.

The solution was to change the compiler to this. ( https://sourceforge.net/projects/mingw-w64/ )

And you probably need to create a path and then rewrite tasks.json and launch.json

Here are my tasks.json and launch.json as an example. Please refer to it.

{
    "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 アクティブなファイルのビルド"
        }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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