【问题标题】:How can I pass a file into my program, when debugging it with vs code使用 vs 代码调试文件时,如何将文件传递到我的程序中
【发布时间】:2018-04-14 18:25:36
【问题描述】:

我想启动 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,我已经尝试使用"&lt;", "input.txt",就像this post"&lt;input.txt" 中所建议的this post 一样。两者都不起作用。正如here 所建议的那样,在cmd 上进行调试似乎是一种非常糟糕的做法。当我拥有强大的 vs code 调试工具时,为什么还要在 cmd 上进行调试?

我在 Windows 机器上运行。

【问题讨论】:

  • 如果你给我一个-1,你能不能也给我一个答案?
  • 您找到解决方案了吗?我陷入了同样的困境。

标签: c++ windows debugging visual-studio-code exe


【解决方案1】:

您缺少的是告诉 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"
        }
    ]
}

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多