求助为什么从txt里读取数据生成链表时,第一串姓名读取失败

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");
}

用这种方式就可以正常显示了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值