c - 使用 fread() 时出现段错误

标签 c struct segmentation-fault fread

我在调用 fread() 时遇到段错误,但我无法弄清楚错误可能出在哪里:

`void charger(Repertoire* parent){
    int i;
    if (parent == 0){parent = &source;}
    FILE* rep_file = fopen("repertoires.bin", "rb");
    fread(parent, sizeof(Repertoire), 1, rep_file); //<-------------- here
    fclose(rep_file);
    //more code...`

作为引用,我的函数 charger() 在此处调用:

`int main(int argc, char* argv[]){
    int i, j, path = 0;
    source.nom = "/.";
    disque = (char*) calloc(32000, sizeof(char));
    charger(0);
    //more code...`

Repertoire 是在头文件中定义的(构建器在 .c 文件中初始化):

`typedef struct repertoire{
    int taille;
    int itaille;
    char* nom;
    struct repertoire* enfants;
    int countup;
    int icountup;
    struct inode* fichiers;
} Repertoire;
Repertoire source;
//more code...` 

有人可以帮忙吗?

最佳答案

我试图对不存在的文件使用 fread,这触发了段错误。我以为 fopen (带有读取选项)允许我创建一个文件,但这只能通过写入和追加来完成。

关于c - 使用 fread() 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42742091/

相关文章:

c - SDL_CreateRenderer 内的段错误

c - stdarg.h 功能似乎错误地连接参数

c - 重复程序提示

c - C 线程编程中未正确获取全局变量

iphone - NSDateComponents包装在结构中?

C - 将值插入到结构中

c++ - c++中的段错误(核心转储)

C 操作目录 : how to position at a directory by giving its name in main arguments

Golang - 将 "inheritance"添加到结构中

c - 读取文件时出现段错误