tea_node_t* Createteacher()//新建链表
{
tea_list_t p=(tea_list_t)malloc(sizeof(tea_node_t));
if(p==NULL){
printf("create error!\n");
return NULL;
}
p->next=NULL;
tea_list_t pfile=NULL;
tea_list_t q=NULL;
q=p;
char name[20],password[20],sex[1];
double number;
int birth;
FILE *fp;
fp = fopen("teacher.txt", "r");
if (fp == NULL) {
printf("打开文件失败!");
return 0;
}
// 逐行读取txt文件中的数据,将读取到的数据存储到链表节点中
while (fscanf(fp,"%s%lf%s%s%d",name,&number,password,sex,&birth) != EOF) {
pfile=(tea_list_t)malloc(sizeof(tea_node_t));
if(pfile==NULL){
printf("pfile error!\n");
return NULL;
}
strcpy(pfile->data.name,name);
strcpy(pfile->data.password,password);
strcpy(pfile->data.sex,sex);
pfile->data.number=number;
pfile->data.birth=birth;
q->next=pfile;
q=pfile;
}
// 关闭txt文件
fclose(fp);
return p;
}
while (!feof(fp))
{
t = (struct stuInfo*)malloc(sizeof(struct stuInfo));
t->next = 0;
t->id[0] = 0;
fscanf(fp, "%s %s %f", t->id,t->name,&t->score);
if (strlen(t->id) == 0)
{
free(t);
}
else
{
p->next = t;
p = t;
}
}
fclose(fp);
printf("文件读取成功!\n");
}
用这种方式就可以正常显示了