我在调用 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/