【发布时间】:2021-01-03 13:29:14
【问题描述】:
我发现了这个问题,但它已关闭https://github.com/microsoft/vscode/issues/112165
问题是VSCode在调试c++代码时无法从文件中读取数据。
复制步骤:
- 创建 .cpp 文件
- 用一些数据创建一个 .txt
- 使用freopen或fopen,从.txt文件中读取数据
- 调试中
- 变量永远不会改变它的值
T 的值应该是 3(它在常规执行中),但在调试时它是一个随机值 16。
一切都是默认的
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe\""
}
]
}
我用
#ifdef SHERAJ
freopen("input.txt" , "r", stdin);
#endif
Mac 上的竞争性编程和 VS Code 调试器运行得非常好。请告诉我解决此问题的方法,因为我最近从 Mac 切换到了 Windows。
【问题讨论】:
-
你确定
cwd在你的launch.json 中是正确的吗? -
cwd是自动生成的。无论如何我浏览了C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin存在。调试器中一切正常,只有在不使用 freopen 时。 -
但我希望
cwd设置为input.txt所在的目录。 -
哇,我更改了
cwd,它成功了!我非常感谢你@ssbssa -
我是在 stackoverflow 上发布问题的新手,我对它的帮助感到非常惊讶。我很好奇,你为什么要帮我@ssbssa,这对你有什么好处?
标签: c++ windows debugging visual-studio-code gdb