注册 登录
编程论坛 C语言论坛

读取txt文件内容到链表中换行问题

加菲不成猫 发布于 2022-04-30 16:19, 1132 次点击
问题:读取txt内容到链表只能读取第一行,然后死循环

程序代码:
#include<stdio.h>
#include<stdlib.h>

struct sing{
    struct sing *next;
    int num;
    char name[20];
    float score[6];
};

void menu()
{
   
}
struct sing *creat()
{
    char a[1000];
    struct sing *head;
    struct sing *p1,*p2;
    p1=p2=(struct sing*)malloc(sizeof(struct sing));
    head=p1;
    FILE *fp=fopen("D:\\123\\sing.txt","r");
    fseek(fp,67,0);
    while(!feof(fp))
    {   
        fscanf(fp,"%d%s%f%f%f%f%f%f",&p1->num,&p1->name,&p1->score[0],&p1->score[1],&p1->score[2],&p1->score[3],&p1->score[4],&p1->score[5]);
        p2->next=p1;
        p2=p1;
        p1=(struct sing *)malloc(sizeof(struct sing));
        
    }
    p2->next=NULL;
    fclose(fp);
    return(head);
}

void print(struct sing *head)
{
    struct sing*p;
    p=head;
    while(p!=NULL)
    {
        printf("%d\t%s\t%f\t%f\t%f\t%f\t%f\t%f\n",p->num,p->name,p->score[0],p->score[1],p->score[2],p->score[3],p->score[4],p->score[5]);
        p=p->next;
    }
}

int main()
{
    struct sing*head;
    head=creat();
    print(head);
}

txt文本内容:
num    name        score1    score2    score3    score4    score5    score6    total    average
1    zhangsan     100.00     90.00     50.00     60.00     45.00     66.00
2    lisi          66.00     55.00     77.00     88.00     100.00     69.00
3    lili          77.00     58.00     96.00     47.00     88.00     66.00
4    liming        45.00     66.00     88.00     99.00     100.00     67.00
5    liwu          55.00     66.00     88.00     99.00     45.00     99.00
6    qiqi          54.00     65.00     68.50     99.00     100.00     45.00
7    wangming      66.00     77.00     88.00     99.00     100.00     55.00
8    wuyong       54.00     66.00     88.00     99.00     100.00     66.00
9    zhangfeng     66.00     88.00     99.00     100.00     55.00     44.00
10    pengpeng      55.00     66.00     88.00     99.00     100.00     54.00
后面的俩个是总分和平均分,代码是仿照书上改写的
问问在fscanf文本第一行后如何实现换行继续读取文本内容到链表上面?

[此贴子已经被作者于2022-4-30 19:07编辑过]

2 回复
#2
加菲不成猫2022-04-30 16:23
程序输出是:1    zhangsan     100.00     90.00     50.00     60.00     45.00     66.00一直死循环
#3
加菲不成猫2022-04-30 19:08
打扰了,解决了
1