Use input (stdin) in debug console VScode
Asked Answered
V

2

12

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.

enter image description here

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.

enter image description here

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"
}
Valet answered 11/11, 2020 at 12:9 Comment(2)
The problem seems to be piping input to stdin. Not sure if this link might help: github.com/microsoft/vscode-go/issues/219Outside
@Outside thank you, but it didn't help me and I have found another answer.Valet
P
27

In launch.json, you can specify a file to connect to stdin via:

"args": ["<", "/path/to/your/stdin/file"]
Prebend answered 27/10, 2021 at 19:43 Comment(3)
Thanks. I also use ${workspaceFolder} in "args": ["<", "${workspaceFolder}/myfile"]Hassiehassin
If you use this technique with 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.Prebend
Related docs: code.visualstudio.com/docs/editor/…Preussen
T
4

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.

Thermodynamic answered 14/3 at 18:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.