【问题标题】:VS Code debugging issue when reading data from a file in C从 C 中的文件读取数据时出现 VS Code 调试问题
【发布时间】:2021-02-04 14:43:57
【问题描述】:

我正在尝试在 VS Code 中调试我的代码。当我“运行”代码(右上角的按钮或 Ctrl+Alt+N)时,它运行良好。但是当我尝试调试时,代码转到 flow==NULL 选项。

这是我的代码阅读功能:

AVLTree readData(char* file_name){
    
    AVLTree myTree;
    myTree = CreateTree();
    
    /* Opening file stream for reading data from the file */
    FILE* flow;
    flow = fopen(file_name,"r");
    if(flow==NULL){
        printf("\nflow == NULL option\n");
        printf("File Error"); exit(1);
     }
    else{
        printf("File opened successfully\n");
     }

这也是我的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": "gcc.exe - Etkin dosyayı derle ve dosyada hata ayıkla",
            "type": "cppdbg",
            "request": "launch",
           // "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "program": "${fileDirname}\\indexingphotos.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": "gdb için düzgün yazdırmayı etkinleştir",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc.exe etkin dosyayı derle"
        }
    ]
}

我该怎么办?

另外,我是一名计算机工程专业的学生,​​直到现在我一直在使用 Dev C++。我正在尝试切换到 VS CODE,但它似乎比 Dev C++ 更令人困惑。我如何学习使用 VS Code(或其他现代 IDE)有什么建议吗?

【问题讨论】:

  • 如果找不到该文件,那么可能程序是在错误的目录中启动的。使用绝对路径或 cd 到程序中的正确目录。
  • 我该怎么做?能解释一下吗,谢谢。
  • 您使用的输入文件的路径可能是相对的。如果是这样,请确保您的程序在同一目录中以“运行”和“调试”模式启动,以便路径正确识别文件; - 或 - 使用文件的绝对路径,以便无论程序的当前目录是什么都可以找到它。

标签: c visual-studio-code vscode-debugger


【解决方案1】:

您可以尝试一件事:将“launch.json”中“cwd”(当前工作目录)的值更改为您要读取文件的文件夹。例如,如果你的文件要在路径“C:\Users\username\Documents\file.txt”中读取,只需将“cwd”:“C:\Users\username\Documents”,然后编译并运行再来一次。

“launch.json”文件控制 C++ 调试器的参数,您可以在 VS 代码 here 的参考资料中找到包括“cwd”在内的此类参数的完整列表。您无法读取文件的一个可能原因是您的程序找不到该文件,因为您的文件不在程序的搜索路径中。

我刚才遇到了同样的问题,并尝试通过设置当前工作目录来解决它,尽管我不确定这是否是最好的方法。更好的方法可能是运行带有参数的调试器以将新目录添加到搜索路径(就像您可以在“task.json”中使用 -I 参数执行的操作一样)。我找不到 gdb 的参数。但有一件事是肯定的:你必须让程序或调试器以某种方式找到文件。

【讨论】:

    猜你喜欢
    • 2021-12-20
    • 2020-07-28
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 2021-03-11
    • 2016-05-08
    • 1970-01-01
    相关资源
    最近更新 更多