I try to debug some code in vs code. Everything works fine, but when I trying input something in console nothing happens.
my code:
#include <stdio.h>
#define SIZE 20
int main()
{
char arr[SIZE];
fgets(arr, SIZE, stdin);
puts(arr);
return 0;
}
As you can see, I use fgets() to read string from console, but when the debug reaches the function everything stops and I can't enter anything.
Just black window.
But, when I use gets(), like in this exemple:
#include <stdio.h>
#define SIZE 20
int main()
{
char arr[SIZE];
gets(arr);
puts(arr);
return 0;
}
Everything works properly.
Where is the problem? Maybe debug console can't read anything? How can I input anything using fgets()?
Here is my 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"
}
]
}
and 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"
}
-
The problem seems to be piping input to stdin. Not sure if this link might help: github.com/microsoft/vscode-go/issues/219Cyclonecode– Cyclonecode2020年11月11日 12:39:57 +00:00Commented Nov 11, 2020 at 12:39
-
@Cyclonecode thank you, but it didn't help me and I have found another answer.senoron– senoron2020年11月11日 17:43:20 +00:00Commented Nov 11, 2020 at 17:43
2 Answers 2
In launch.json, you can specify a file to connect to stdin via:
"args": ["<", "/path/to/your/stdin/file"]
4 Comments
${workspaceFolder} in "args": ["<", "${workspaceFolder}/myfile"]mkfifo I am curious about your experience. I think it is possible to debug with interactive input, e.g. echo interactive input >> namedpipe but I haven't tried it.Another approach (at least using VSCode 1.87.2) is to specify the console option.
"console": "integratedTerminal"
This will output your program in the terminal rather than Debug Output, which should allow user interaction while debugging.
Comments
Explore related questions
See similar questions with these tags.