【问题标题】:VS Code on Windows debugger does not work with freopenWindows 调试器上的 VS Code 不适用于 freopen
【发布时间】:2021-01-03 13:29:14
【问题描述】:

我发现了这个问题,但它已关闭https://github.com/microsoft/vscode/issues/112165

问题是VSCode在调试c++代码时无法从文件中读取数据。

复制步骤:

  1. 创建 .cpp 文件
  2. 用一些数据创建一个 .txt
  3. 使用freopen或fopen,从.txt文件中读取数据
  4. 调试中
  5. 变量永远不会改变它的值

截图:

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


【解决方案1】:

好的,我知道这里发生了什么。在launch.json文件中,“cwd”值是正在启动或调试的进程的当前工作目录的路径,这是默认读取输入文件和输出文件并写入的位置。

您应该将其设置为您正在使用的文件所在的目录。

here 的回答。类似问题:freopen() does not read / write to existing file, niether creates new file in vscode

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-19
    • 2021-01-06
    • 2018-11-30
    • 2022-11-05
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多