C++ Windows调试器上的VS代码不能与freopen一起使用
我发现了这个问题,但它已经关闭了 问题是,VSCode在调试C++代码时不能从文件中读取数据。 复制步骤:C++ Windows调试器上的VS代码不能与freopen一起使用,c++,windows,debugging,visual-studio-code,gdb,C++,Windows,Debugging,Visual Studio Code,Gdb,我发现了这个问题,但它已经关闭了 问题是,VSCode在调试C++代码时不能从文件中读取数据。 复制步骤: 创建一个.cpp文件 用一些数据创建一个.txt文件 使用freopen或fopen,从.txt文件读取数据 调试 变量永远不会改变其值 截图: T的值应该是3(这是在常规执行下),但在调试下它是一个随机值16 一切都是默认的 launch.json { // Use IntelliSense to learn about possible attributes. //
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\""
}
]
}
我用
对于有竞争力的编程和Mac上的VS代码,调试器运行得非常好。请告诉我此问题的解决方法,因为我最近从Mac切换到Windows。您确定
cwd
在您的启动中是正确的。json?cwd
是自动生成的。无论如何,我浏览了C:\Program Files\mingw-w64\x86\u 64-8.1.0-posix-seh-rt\u v6-rev0\mingw64\bin
。只有在不使用freopen的情况下,调试器中的一切都可以正常工作。但是我希望cwd
被设置到input.txt
所在的目录中。哇,我更改了cwd
,它成功了!非常感谢@ssbssaI,我刚开始在stackoverflow上发布问题,我对它的帮助感到非常惊讶。我很好奇,你为什么帮我@ssbssa,里面有什么东西给你?你确定cwd
在你的发布中是正确的。json?cwd
是自动生成的。无论如何,我浏览了C:\Program Files\mingw-w64\x86\u 64-8.1.0-posix-seh-rt\u v6-rev0\mingw64\bin
。只有在不使用freopen的情况下,调试器中的一切都可以正常工作。但是我希望cwd
被设置到input.txt
所在的目录中。哇,我更改了cwd
,它成功了!非常感谢@ssbssaI,我刚开始在stackoverflow上发布问题,我对它的帮助感到非常惊讶。我很好奇,你为什么帮我@ssbssa,这对你有什么好处?
#ifdef SHERAJ
freopen("input.txt" , "r", stdin);
#endif