我的程序中有以下四个结构
struct SType{
int type;//struct type
};
struct S1{
};
struct S2{
};
struct S3{
};
我使用以下代码将这些结构的状态保存在一个文件中:
无效存储(结构 SType s1,无效 *s){
//open file and stuff //s points to either one of the last three structs fwrite(&s1,sizeof(s1),1,file); fwrite(s, size, 1, file); //structs are always saved in the file in pairs of SType and either one of the last three structs }
现在,当我尝试使用以下代码从文件中检索该对的第二个结构时,出现分段错误。那么如何使用 fread() 检索任意结构类型的对象?
void read(){
void *record;
//read struct SType object from the file
//now read the second struct of the pair
fread(record,size,1,file);
}