14

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"
}
2
  • The problem seems to be piping input to stdin. Not sure if this link might help: github.com/microsoft/vscode-go/issues/219 Commented Nov 11, 2020 at 12:39
  • @Cyclonecode thank you, but it didn't help me and I have found another answer. Commented Nov 11, 2020 at 17:43

2 Answers 2

33

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

"args": ["<", "/path/to/your/stdin/file"]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I also use ${workspaceFolder} in "args": ["<", "${workspaceFolder}/myfile"]
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.
This doesn't seem to work on Windows, although it was probably foolish of me to think that it would, since it's Unix shell syntax.
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.