#define COLSIZE (sizeof(struct col)-sizeof(struct col *)-sizeof(struct cour *))
#define COUSRSIZE (sizeof(struct cour)-sizeof(struct cour *)-sizeof(struct res *))
#define RESSIZE (sizeof(struct res)-sizeof(struct res *))
void save_file(struct col *head)
{
struct col *p1;
struct cour *p2;
struct res *p3;
FILE *fp1, *fp2, *fp3;
if ((fp1 = fopen("d:\\college.dat", "wb")) == NULL)
{
printf("Can't open the file.");
exit(-1);
}
if ((fp2 = fopen("d:\\course.dat", "wb")) == NULL)
{
printf("Can't open the file.");
exit(-1);
}
if ((fp3 = fopen("d:\\result.dat", "wb")) == NULL)
{
printf("Can't open the file.");
exit(-1);
}
for (p1 = head; p1 != NULL; p1 = p1->next)
{
fwrite(p1, COLSIZE, 1, fp1); /*
* 保存学院信息
*/
for (p2 = p1->first; p2 != NULL; p2 = p2->next)
{
fwrite(p2, COUSRSIZE, 1, fp2); /*
* 保存课程信息
*/
for (p3 = p2->first; p3 != NULL; p3 = p3->next)
{
fwrite(p3, RESSIZE, 1, fp3); /*
* 保存课程详细信息
*/
}
}
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
struct col *load_file()
{
struct col *head = NULL, *p, *tail1;
struct cour *q, *tail2;
struct res *r, *tail3;
unsigned i, j;
FILE *fp1, *fp2, *fp3;
if ((fp1 = fopen("d:\\college.dat", "rb")) == NULL)
{
printf("can't open the file");
exit(-1);
}
if ((fp2 = fopen("d:\\course.dat", "rb")) == NULL)
{
printf("can't open the file");
exit(-1);
}
if ((fp3 = fopen("d:\\result.dat", "rb")) == NULL)
{
printf("can't open the file");
exit(-1);
}
while (!feof(fp1) && !ferror(fp1))
{
p = (struct col *) malloc(sizeof(struct col));
fread(p, COLSIZE, 1, fp1);
if (!feof(fp1) && !ferror(fp1))
{
if (head == NULL)
head = p;
else
tail1->next = p;
tail1 = p;
q = (struct cour *) malloc(sizeof(struct cour));
fread(q, COUSRSIZE, 1, fp2);
p->first = q;
tail2 = q;
r = (struct res *) malloc(sizeof(struct res));
fread(r, RESSIZE, 1, fp3);
q->first = r;
tail3 = r;
for (j = 1; j < q->n; j++)
{
r = (struct res *) malloc(sizeof(struct res));
fread(r, RESSIZE, 1, fp3);
tail3->next = r;
tail3 = r;
}
tail3->next = NULL;
for (i = 1; i < p->m; i++)
{
q = (struct cour *) malloc(sizeof(struct cour));
fread(q, COUSRSIZE, 1, fp2);
tail2->next = q;
tail2 = q;
r = (struct res *) malloc(sizeof(struct res));
fread(r, RESSIZE, 1, fp3);
q->first = r;
tail3 = r;
for (j = 1; j < q->n; j++)
{
r = (struct res *) malloc(sizeof(struct res));
fread(r, RESSIZE, 1, fp3);
tail3->next = r;
tail3 = r;
}
tail3->next = NULL;
}
tail2->next = NULL;
}
}
tail1->next = NULL;
fclose(fp1);
fclose(fp2);
fclose(fp3);
return (head);
}