[英]How can I read from stdin using the integrated Visual Studio Code terminal?
I have the following C application:我有以下 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;
}
I am running it with:我正在运行它:
When I hit CTRL+F5 (effectively, Run Without Debugging ), I see the prompt, What is your name?
当我按 CTRL+F5 (实际上是Run without Debugging )时,我看到提示,
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.
我尝试在调试控制台中输入我的名字,它给出了以下错误:
Unable to perform this action because the process is running.
How can I read from stdin
using the integrated terminal?如何使用集成终端从
stdin
读取?
Here is my launch.json
configuration:这是我的
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"
}
]
}
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.通过在上述配置中设置
"externalConsole": true,
我能够使用外部控制台从stdin
输入获取输入,但我正在寻找一种方法,如果可能的话,能够简单地将我的名字写在一个集成的 IDE 控制台选项卡,这样我就不需要启动外部控制台。
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 )
( 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/ )
( https://sourceforge.net/projects/mingw-w64/ )
And you probably need to create a path and then rewrite tasks.json and launch.json你可能需要创建一个路径,然后重写tasks.json和launch.json
Here are my tasks.json and launch.json as an example.这里以我的 tasks.json 和 launch.json 为例。 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 アクティブなファイルのビルド"
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.