How can I pass a file into my program, when debugging it with vs code
Asked Answered
B

3

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.

Biforked answered 14/4, 2018 at 18:25 Comment(2)
if you give me a -1, could you also give me an answer?Biforked
Did you manage to find a solution? I am stuck in the same thing.Pasco
I
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"
        }
    ]
}
Inconsecutive answered 13/11, 2020 at 1:41 Comment(0)
P
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.

Puppis answered 18/2, 2022 at 3:25 Comment(0)
B
0

Add this to launch.json. It works for me.

"setupCommands": [
    {
        "text": "settings set target.input-path ${workspaceFolder}/input.txt"
    }
  ]
Brunt answered 21/5 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.