Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

VS Code debugging issue when reading data from a file in C

I'm trying to debug my code in VS Code. When I "Run" the code (the button on the top right corner or Ctrl+Alt+N) it works perfectly. But when I try debug, the code goes to the flow==NULL option.

Here is my reading function from code:

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("
flow == NULL option
");
        printf("File Error"); exit(1);
     }
    else{
        printf("File opened successfully
");
     }

Also here is my 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"
        }
    ]
}

What should I do?

Also, I'm a computer engineering student and I worked with Dev C++ until now. I'm trying to switch to VS CODE, but it seems too confusing than Dev C++. How can I learn to use VS Code (or another modern IDE) any suggestions?

question from:https://stackoverflow.com/questions/66047895/vs-code-debugging-issue-when-reading-data-from-a-file-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is one thing you can try: change the value of "cwd"(current working directory) in "launch.json" to the folder where you have your file to read. For example, if you have your file to read in the path "C:UsersusernameDocumentsfile.txt", just put the "cwd": "C:UsersusernameDocuments", then compile and run it again.

The "launch.json" file controls the arguments for your C++ debugger and you can find a full list of such arguments including "cwd" in the reference of VS code here. One possible reason why you cannot read the file is that your program cannot find the file because your file is not in the search path of your program.

I had the same issue just now and tried to solve it by setting the current working directory although I'm not sure if that's the best way to do it. A better way is probably to run debugger with argument to add new directory to the search path(like what you can do in "task.json" using -I argument). I was not able to find an argument for gdb. But one thing is for sure: you have to let the program or debugger find the file in some way.

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...