C语言 读文件进链表 显示有多余的 屯屯屯屯屯屯
#include<stdio.h>#include<stdlib.h>
#include<fstream>
using namespace std;
struct students
{
char no[20];
char name[20];
char score;
struct students *next;
};
int main(){
FILE *fp=NULL;
students *p,*p1;
students *head;
head = (students *)malloc(sizeof(students));
head->next = NULL;
if((fp = fopen("student.txt","rb"))==NULL)
{
printf("cannot open flie\n");
return 0;
}
while(!feof(fp))
{
p = (students *)malloc(sizeof(students));
fread(p,sizeof(students),1,fp);
if(head==NULL) {
head=p;
p->next=NULL;
}
else{
p1=head;
while(p1->next!=NULL){
p1=p1->next;
}
}
p1->next = p;
p->next = NULL;
}
fclose(fp);
while (head != NULL) //输出链表中内容,验证数据已经写入到链表
{
printf ("s% s% d% ",head->no,head->name,head->score);
head = head->next;
}
printf ("数据写入链表成功\n");
return 0;
}