int n;
if ((fp = fopen("student.txt", "a")) == NULL)
{
printf("File open error!\n");
exit(1);
}
fclose(fp);
if ((fp = fopen("student.txt", "r")) == NULL)
{
printf("File open error!\n");
exit(1);
}
p1 = (struct student*)malloc(sizeof(struct student));
p1->next = NULL;
head = p1;
while (!feof(fp))
{
if (fread(p1, sizeof(struct student), 1, fp) != 1)
break;
p2 = (struct student*)malloc(sizeof(struct student));
p2->next = NULL;
p1->next = p2;
p1 = p2;
}
}
首先判断student.txt是否可读可写,然后将txt文件中按照结构体student的内容分别读出并保存在指针p1内存上。
这段代码有两个可能存在的问题,让它看起来根本没有意义:
用来读取文件student.txt,将结果放到结构体中