简体   繁体   English

fread 从 c 中的二进制文件读取数据库分段错误

[英]fread segmentation fault reading database from binary file in c

I am writing a function, that helps me read a database of books from a binary file.我正在写一个 function,它可以帮助我从二进制文件中读取书籍数据库。 However when using fread i am having a segmentation fault.但是,当使用fread时,我遇到了分段错误。

This is the function I am talking about.这就是我说的function。

Array *readdb (FILE *db, Array *ind) {
    Array *database = NULL;
    Book *book = NULL;
    indexbook *indentry;
    char *buffer, bytesdepillar = 0, titprintby[MAX_ENTRADA];
    int reader = 0;

    if (db == NULL || ind == NULL) 
        return NULL;

    database = malloc(sizeof(Array));

    initArray(database, INIT_ARR_SIZE);

    fseek(db, 0L, SEEK_END);
    bytesdepillar = ftell(db)/sizeof(char);
    fseek(db, 0, SEEK_SET);
    buffer = malloc(bytesdepillar * sizeof(char));
    fread(buffer, sizeof(char), bytesdepillar, db);
    
    for (size_t i = 0; i < ind->used; i++) {
        book = malloc(sizeof(Book));
        indentry = (indexbook*) ind->array[i];

        reader = indentry->offset;

        book->bookID = indentry->key;
        reader+=sizeof(int);
        memcpy(book->ISBN, &buffer[reader], ISBN_LEN);
        reader+=ISBN_LEN;
        memcpy(titprintby, &buffer[reader], indentry->offset + indentry->size - reader);
        strcpy(book->title, strtok(titprintby, "|"));
        strcpy(book->printedBy, strtok(NULL, "|"));
    
        insertArray(database, book);
    }

    return database;
}

ind is the database index with the following structure: ind 是具有以下结构的数据库索引:

typedef struct {
  int key;
  long int offset;
  size_t size; 
} indexbook;

The file is being opened with该文件正在打开

    if ((dbf = fopen(db_name, "rb")) == NULL) {
        fclose(indf);
        printf("%s does not exist", db_name);
        return 1;
    }

As you have stated in the comments section, when the line正如您在评论部分所述,当该行

fread(buffer, sizeof(char), bytesdepillar, db);

is executed, the variable bytesdepillar has a negative value.执行时,变量bytesdepillar为负值。

Since you declared the variable to be of type char , depending on your platform, it is probably only able to represent values between -128 and 127 .由于您将变量声明为char类型,根据您的平台,它可能只能表示-128127之间的值。 If the call to ftell returns a value that is higher than 127 , it will probably (it is implementation-defined what exactly happens) be converted to a value between -128 and 127 .如果对ftell的调用返回一个高于127的值,它可能(它由实现定义到底发生了什么)被转换为介于-128127之间的值。 That is most likely the reason why it is a negative value.这很可能是它为负值的原因。

Therefore, I recommend that you change the declared type of bytesdepillar from char to long , which is the type returned by the function ftell .因此,我建议您将声明的bytesdepillar类型从char更改为long ,也就是 function ftell返回的类型。

If, after changing the declared type to long , the value continues to be negative, then it is probably due to the function ftell returning -1 due to the function failing.如果将声明的类型更改为long后,该值仍然为负,则可能是由于 function 失败而导致 function ftell返回-1 In that case, you should verify that db refers to a valid stream (eg verify that the file that has been successfully opened).在这种情况下,您应该验证db是否引用了有效的 stream(例如验证文件是否已成功打开)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM