vscode运行代码出错ERROR: GDB exited unexpectedly. Debugging will now abort.

用Ubuntu20.04.5的vscode运行一个生成数据的代码,运行到图示位置时出错:
ERROR: GDB exited unexpectedly. Debugging will now abort.
The program '/home/zhang/project/MSFinal-master/BDMRS/gen_data' has exited with code -1 (0xf.).
经过断点运行判断是执行完循环后就出错,各位知道是什么原因吗?

img

这是define.h文件

/***
 * This code .h file define size and struct used.
 */

#define FILE_SIZE       500
// only 26*n is support due to invert matrix function
// only 26 and 676 add to git
#define DICT_SIZE       650
#define FILE_PATH       "../doc/FILE0001.txt"
#define MATRIX_PATH     "../Matrix/Matrix650trans.txt"
#define MATRIXinv_PATH  "../Matrix/Matrix650inv.txt"
#define SK_PATH         "./SK.txt"

// struct of node
typedef struct tree_node {
    int ID; // node ID
    double D[2][DICT_SIZE]; // index data
    struct tree_node *Pl; // pointer to left node
    struct tree_node *Pr; // pointer to right node
    int FID; // pointer to file, use file's ID here
} Node;

// max function
double max( double a, double b ){ return (a>b)?a:b; }

这是出现问题的gen_data.c文件

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "define.h"

/***
 * This code generate the data used in search
 */

int main() {
    int i, j;
    FILE *fp;
    struct stat st = {0};

    // initial the random seed
    srand( time(NULL) );
    // if no dir "doc", creat it
    if (stat("./doc", &st) == -1) {
        mkdir("./doc", 0700);
    }

    // for each file
    for( i = 0; i < FILE_SIZE; i++ ) {
        //initial file name
        char filename[] = FILE_PATH;
        char *ptr = strstr(filename, "FILE");
        sprintf(ptr, "FILE%04d.txt", i+1);
        fp = fopen(filename, "w");

        // for each keyword in file
        for( j = 0; j < DICT_SIZE; j++ ) {
            // initial keyword name
            char word[] = "aa";
            word[0] = 'a' + j / 26;
            word[1] = 'a' + j % 26;

            // generate keyword size, half set to 0
            int a = rand()%(DICT_SIZE*2) - DICT_SIZE;
            if( a < 0 ) a = 0;
            fprintf(fp, "%s %d\n", word, a);
        }

        // close file
        fclose(fp);
    }

    return 0;
}

两个地方改改试一下
第一个判断fp是否打开文件成功 函数调用最好判断下返回值
第二个设置word[2] = '\0'

文件操作有需要注意的地方,你依次检查一下下面的情况:
1、open文件要对文件描述符或文件指针进行判断,保证成功打开才能进行读写操作。
2、打开文件失败,检查文件路径是否包含不存在的目录(文件夹),需要手动创建或者在程序里面先创建完整的目录路径,因为open和fopen这些函数只可以创建新文件,但不能创建目录。
3、检查权限,保证程序有创建、修改文件的权限,比如在根目录中,一般情况只有root才有权限操作文件的。

是不是文件被占用了,比如应用程序和外部程序都要打开同一个文件。

ERROR: GDB exited unexpectedly. Debugging will now abort.
错误:GDB意外退出。调试现在将中止。
思路:
1、使用gdb命令行调试,看看是不是gdb的问题
2、在ubuntu虚拟机上用VSCdebug调试,看看是不是平台的问题
详情请查看这篇博文的问题分析的思路,非常清晰,可以借鉴,助你进一步分析原因:https://www.cnblogs.com/Baiyug/p/16243436.html