繁体   English   中英

使用vs代码调试文件时,如何将文件传递到我的程序中

[英]How can I pass a file into my program, when debugging it with vs code

我想用一个传递给它的文件来启动 a.exe(一个编译的 cpp 程序)。 在 CMD 上,我会像a.exe < input.txt那样做。 如何在调试模式下使用 VS Code 启动它?

这是我的 launch.json 文件:

{
"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"
        }
    }
]}

作为args ,我已经尝试使用"<", "input.txt" (如本文中所示)和"<input.txt" (如本文中建议的那样)。 两者都不起作用。 按照此处的建议,在 cmd 上进行调试似乎是一种非常糟糕的做法。 当我拥有 vs code 这么棒的调试工具时,为什么还要在 cmd 上调试?

我在 windows 机器上运行。

您缺少的是告诉 gdb 文件实际所在的位置。 在尝试提供输入文件时,您需要将文件的位置放在引号中,因此它看起来像这样:

{
    "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"
        }
    ]
}

另一种选择是将这些行添加到您的 a.cpp 文件中。

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

所有 cin 和 cout 都将使用 input.txt 和 output.txt。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM