[英]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.