有关文件合并的问题~

现在有3个结构体变量struct Student、struct Course、struct Attend创建的链表分别储存在3个文件中,文件名分别为stu.dat、course.dat、attend.dat,如何将这3个文件的内容合并储存在1个文件当中,并可以正确从中读取数据,跪求各路大神解答~小弟在此谢过~最好有详细代码。
以下是一些数据:
#define LEN_stu sizeof(struct Student)
#define LEN_course sizeof(struct Course)
#define LEN_attend sizeof(struct Attend)
struct Student
{char name[16];
int age;
struct Student *next;
};
struct Course
{ float credit;
char teacher[16];
struct Course *next;
};
struct Attend
{ char attend[12];
char place[20];
struct Attend *next;
};

#define LEN_stu sizeof(struct Student)
#define LEN_course sizeof(struct Course)
#define LEN_attend sizeof(struct Attend)

typedef struct stumsg { // 声明一个新的结构类型,包含以上三个结构的所有变量
char name[20];
int age;
float credit;
char teacher[16];
char attend[12];
char place[20];
struct stumsg *next;
};
.............................
FILE *fstudent, *fcourse, *attend, *fstumsg;
struct Student s;
struct Course c;
struct Attend a;
struct stumsg msg;
// 以读方式打开二进制文件stu.dat、course.dat、attend.dat
// 以写方式打开二进制文件stumsg.dat
// 三个输入文件的记录数应该相等,只需判断任意一个文件是否结束即可
while(fread(&s,LEN_stu,1,fstudent) == 1) {
fread(&c,LEN_course,1,fstudent);
fread(&a,LEN_attend,1,fstudent);
strcpy(msg.name,s.name);
// ...............逐个复制到msg中
fwrite(&msg,sizeof(stumsg),1,fstumsg);
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-05-29
定义个结构体,
struct StuInfo
{
struct Student stu;
struct Course cos;
struct Attend atd;
};
写入:
fwrite(szBuf,sizeof(struct StuInfo),1,fp);
读出:
fread(szBuf,sizeof(struct StuInfo),1,fp);
相似回答