该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
下面附上规范程序段
studentChain *MakeChainFromFile(char *binFileName)
{
size_t C_LEN = sizeof(studentChain);
size_t S_LEN = sizeof(student);
studentChain *head, *a, *b;
student stu = { 0, 0, 0, "", 0.0, { "", 0.0, 0.0, 0.0, 0.0, 0.0} };
FILE *fpBinary;
if ((fpBinary = fopen(binFileName, "rb")) == NULL)
{
printf("\n文件打开错误!\n");
exit(0);
}
else
{
head=a = (studentChain *)malloc(C_LEN);
head->next = NULL;
head->stu = stu;
if (!feof(fpBinary))
{
fread(&stu, S_LEN, 1, fpBinary);
head->stu = stu;
while (!feof(fpBinary))
{
b = (studentChain *)malloc(C_LEN);
a->next = b;
a = b;
fread(&stu, S_LEN, 1, fpBinary);
a->stu = stu;
a->next = NULL;
}
//解决:文件中最后的一组数据被读两遍
for (b = head; b->next != a; b = b->next);//使b指向倒数第二个数据
if (b->stu.num[2] == a->stu.num[2])
{
b->next = NULL;
}
}
else
{
printf("\nError:文件为空。\n");
exit(0);
}
}
return (head);
}