简体   繁体   English

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

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

I would like to start a.exe (a compiled cpp program) with a file passed into it.我想用一个传递给它的文件来启动 a.exe(一个编译的 cpp 程序)。 On the CMD I would do it like a.exe < input.txt .在 CMD 上,我会像a.exe < input.txt那样做。 How can start it with VS Code in debug mode?如何在调试模式下使用 VS Code 启动它?

This is my launch.json file:这是我的 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"
        }
    }
]}

As args I already tried to to use "<", "input.txt" as like in this post and "<input.txt" as suggested in this post .作为args ,我已经尝试使用"<", "input.txt" (如本文中所示)和"<input.txt" (如本文中建议的那样)。 Both do not work.两者都不起作用。 Debugging on the cmd, as suggested here seems like a very bad practice.按照此处的建议,在 cmd 上进行调试似乎是一种非常糟糕的做法。 Why should I debug on a cmd, when I have the awesome debug tools of vs code?当我拥有 vs code 这么棒的调试工具时,为什么还要在 cmd 上调试?

I run on a windows machine.我在 windows 机器上运行。

What you were missing was to tell gdb where the file was actually located.您缺少的是告诉 gdb 文件实际所在的位置。 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"
        }
    ]
}

Another option is to add these lines in your a.cpp file.另一种选择是将这些行添加到您的 a.cpp 文件中。

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

All cin's and cout's will use input.txt and output.txt.所有 cin 和 cout 都将使用 input.txt 和 output.txt。

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

相关问题 为什么我的程序在VS2013中调试时无法打开文件? - Why can't my program open a file when debugging in VS2013? 如何解决:使用gdb调试时VS Code中的“启动:程序“executable.out”不存在” - How to solve : "launch : program "executable.out" does not exist" in VS Code when debugging with gdb 如何将 C 程序的输入传递给在命令行中运行的另一个程序? [等候接听] - How can I pass inputs from my C program to another program running in the command line? [on hold] 如何让 VS Code 在调试时不打开终端? - How to make VS Code not open a terminal when debugging? 如何编写我的程序来查看我的程序的索引7只有两个字母中的一个 - How can I code my program to look at index 7 of my program for only one of two letter 如何将这个qt程序放入一个源代码文件中 - How can I put this qt program into one source code file 使用Code :: Blocks进行调试时提供程序参数 - Provide program arguments when debugging with Code::Blocks 调试时如何在VS Code的集成终端中查看C程序的output - How to view output of C program in VS Code's integrated terminal while debugging 在 Eclipse 中调试 C/C++ 代码时如何查看全局变量? - How can I see global variables when debugging C/C++ code in Eclipse? 在 VScode 中调试时,如何在 docker 容器中找到源代码文件夹 - How can I find source code folder within docker container when debugging in VScode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM