| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 831 人关注过本帖
标题:读取txt文件内容到链表中换行问题
只看楼主 加入收藏
加菲不成猫
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2022-3-13
结帖率:100%
收藏
 问题点数:0 回复次数:2 
读取txt文件内容到链表中换行问题
问题:读取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编辑过]

搜索更多相关主题的帖子: 内容 score 读取 head struct 
2022-04-30 16:19
加菲不成猫
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2022-3-13
收藏
得分:0 
程序输出是:1    zhangsan     100.00     90.00     50.00     60.00     45.00     66.00一直死循环
2022-04-30 16:23
加菲不成猫
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2022-3-13
收藏
得分:0 
打扰了,解决了
2022-04-30 19:08
快速回复:读取txt文件内容到链表中换行问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018244 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved