6

I would like to start a.exe (a compiled cpp program) with a file passed into it. On the CMD I would do it like a.exe < input.txt. How can start it with VS Code in debug mode?

This is my launch.json file:

{
"version": "0.2.0",
"configurations": [
    {
        "name": "C++ Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceRoot}/a.exe",
        "args": [
            "<",
            "input.txt"
        ],
        "stopAtEntry": false,
        "cwd": "${workspaceRoot}",
        "environment": [],
        "externalConsole": true,
        "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
        "linux": {
            "MIMode": "gdb"
        },
        "osx": {
            "MIMode": "lldb"
        },
        "windows": {
            "MIMode": "gdb"
        }
    }
]}

As args I already tried to to use "<", "input.txt" as like in this post and "<input.txt" as suggested in this post. Both do not work. Debugging on the cmd, as suggested here seems like a very bad practice. Why should I debug on a cmd, when I have the awesome debug tools of vs code?

I run on a windows machine.

User12547645
  • 6,955
  • 3
  • 38
  • 69

2 Answers2

0

What you were missing was to tell gdb where the file was actually located. When trying to give an input file you need to put the location of the file into quotes, so it'll look like this instead:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [
                "<", "${fileDirname}/words5.txt"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                    
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}
0

Another option is to add these lines in your a.cpp file.

freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);

All cin's and cout's will use input.txt and output.txt.

Mihir
  • 3
  • 2