我想返回指向我从文件中读取的结构的指针(它转到另一个基本上只是打印数据的函数。)函数通过 int sem 获取(序列号-1),然后的想法是,设置文件指针指向我想用 fseek 读取的结构的开头,然后简单地读取整个结构并返回指针。(示例:如果文件中写入的结构数为5,我想读取4号结构,则函数获取的sem值为3,则用fseek指针跳过前3个结构,函数读取并返回结构 4 号)。如果 sem 值为 0,它可以正常工作,但当我寻找任何其他结构时,它就不那么好了。我不知道出了什么问题!帮助?:)
type_seminar *file_seminars_search(int sem) {
type_seminar *temp_s = (type_seminar*) malloc(sizeof(type_seminar));
FILE *f_sem;
if ((f_sem = fopen("seminars.bin", "r")) != NULL ) {
fseek(f_sem, sem * sizeof(type_seminar), SEEK_SET);
fread(temp_s, sizeof(type_seminar), 1, f_sem);
fclose(f_sem);
return (temp_s);
} else
printf("Cannot access file. \n ");
return 0;
}
结构定义:
typedef struct {
char s_title[A];
char s_street[B];
char s_town[C];
int max_no_teachers;
int no_applied_teachers;
} type_seminar;
如果问题出在此处,则打印数据的功能:
void seminar_details(type_seminar *temp_s) {
printf(" SEMINAR TITLE : %s", temp_s->s_title);
printf(" Street: %s ", temp_s->s_street);
printf(" Town: %s", temp_s->s_town);
printf(" Max no of applied teachers: %d \n", temp_s->max_no_teachers);
printf(" No of applied teachers: %d \n", temp_s->no_applied_teachers);
}