是 秋秋~ 2022-01-03 15:31 采纳率: 100%
浏览 18
已结题

#C语言# #链表# #文件#

通过读取该文件显示到屏幕上来检查stud.txt文件中的数据
读取文件的时候有问题,可以帮忙看一下吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student
{
    char num[10];
    char name[20];
    int sub1;
    int sub2;
    int sub3;
    int ave;
    struct student *next;
};
struct student *head = NULL;

struct student *creat();//创建节点
struct student *save(struct student *head);//保存链表数据到文件
struct student *read();//从文件读取链表
void display_all();//显示所有学生信息

struct student *creat()//创建节点
{
    struct student *p1,*p2;
    p1=p2=(struct student*)malloc(sizeof(struct student));
    printf("请输入学生学号 姓名 语文成绩 数学成绩 英语成绩\n");
    scanf("%s %s %d %d %d",p1->num,p1->name,&p1->sub1,&p1->sub2, &p1->sub3);
    p1->ave = (p1->sub1+p1->sub2+p1->sub3)/3;
    fflush(stdin);
    head=p1;
    for(int i=1;i<3;i++)
    {
        p1=(struct student*)malloc(sizeof(struct student));
        scanf("%s %s %d %d %d",p1->num,p1->name,&p1->sub1,&p1->sub2, &p1->sub3);
        p1->ave = (p1->sub1+p1->sub2+p1->sub3)/3;
        fflush(stdin);
        p2->next=p1;
        p2=p1;
    }
    p2->next=NULL;
    display_all();
    return head;
}
void display_all()//显示所有学生信息
{
    struct student *p;
    p=head;
    if(p == NULL)
        printf("无学生信息!\n");
    else{
        printf("\n学生学号 姓名 语文成绩 数学成绩 英语成绩 平均成绩:\n");
        while(p != NULL)
        {
            printf("%s\t%s\t%d\t%d\t%d\t%d\n\n",p->num,p->name,p->sub1,p->sub2,p->sub3,p->ave);
            p=p->next;
        }
    }
}

struct student *save(struct student *head)//保存链表数据到文件
{
    struct student *p;
    FILE *fp;
    p=head;
    if((fp = fopen("stud.txt","w")) == NULL)
        printf("文件无法打开!\n");
    else
        fp=fopen("stu.txt","w");
    while(p != NULL)
    {
        fprintf(fp,"%s\t%s\t%d \t%d\t%d\t%d\n",p->num,p->name,p->sub1,p->sub2,p->sub3,p->ave);
        p=p->next;
    }
    fclose(fp);
    printf("保存成功!\n");
    return head;
}

struct student *read()//从文件读取链表
{
    struct student *p1,*p2;
    FILE *fp;
    int flag = 1;
    fp=fopen("stud.txt","r");
    printf("学生学号 姓名 语文成绩 数学成绩 英语成绩 平均成绩:\n");
    while(!feof(fp))
    {
        if(flag == 1) ///读取头
        {
            p1=(struct student *)malloc(sizeof(struct student));
            head=p1;
            p2=p1;
            fscanf(fp,"%s %s %d %d %d %d\n",p1->num,p1->name,&p1->sub1,&p1->sub2,&p1->sub3,&p1->ave);
            printf("%s\t%s\t%d\t%d\t%d\t%d\n",p1->num,p1->name,p1->sub1,p1->sub2,p1->sub3,p1->ave);
            flag--;
        }
        else
        {
            p1=(struct student *)malloc(sizeof(struct student));
            fscanf(fp,"%s %s %d %d %d %d\n",p1->num,p1->name,&p1->sub1,&p1->sub2,&p1->sub3,&p1->ave);
            printf("%s\t%s\t%d\t%d\t%d\t%d\n",p1->num,p1->name,p1->sub1,p1->sub2,p1->sub3,p1->ave);
            p2->next=p1;
            p2=p1;
        }
    }
    p2->next = NULL;
    fclose(fp);
    return head;
}

int main()
{
creat();
save(head);
read();
}


  • 写回答

1条回答 默认 最新

  • 书山客 2022-01-03 15:49
    关注
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct student
    {
        char num[10];
        char name[20];
        int sub1;
        int sub2;
        int sub3;
        int ave;
        struct student* next;
    };
    struct student* head = NULL;
    struct student* creat();//创建节点
    struct student* save(struct student* head);//保存链表数据到文件
    struct student* read();//从文件读取链表
    void display_all();//显示所有学生信息
    struct student* creat()//创建节点
    {
        struct student* p1, * p2;
        p1 = p2 = (struct student*)malloc(sizeof(struct student));
        printf("请输入学生学号 姓名 语文成绩 数学成绩 英语成绩\n");
        scanf("%s %s %d %d %d", p1->num, p1->name, &p1->sub1, &p1->sub2, &p1->sub3);
        p1->ave = (p1->sub1 + p1->sub2 + p1->sub3) / 3;
        fflush(stdin);
        head = p1;
        for (int i = 1; i < 3; i++)
        {
            p1 = (struct student*)malloc(sizeof(struct student));
            scanf("%s %s %d %d %d", p1->num, p1->name, &p1->sub1, &p1->sub2, &p1->sub3);
            p1->ave = (p1->sub1 + p1->sub2 + p1->sub3) / 3;
            fflush(stdin);
            p2->next = p1;
            p2 = p1;
        }
        p2->next = NULL;
        display_all();
        return head;
    }
    void display_all()//显示所有学生信息
    {
        struct student* p;
        p = head;
        if (p == NULL)
            printf("无学生信息!\n");
        else {
            printf("\n学生学号 姓名 语文成绩 数学成绩 英语成绩 平均成绩:\n");
            while (p != NULL)
            {
                printf("%s\t%s\t%d\t%d\t%d\t%d\n\n", p->num, p->name, p->sub1, p->sub2, p->sub3, p->ave);
                p = p->next;
            }
        }
    }
    struct student* save(struct student* head)//保存链表数据到文件
    {
        struct student* p;
        FILE* fp;
        p = head;
        if ((fp = fopen("stud.txt", "w")) == NULL)
            printf("文件无法打开!\n");
        else
            fp = fopen("stu.txt", "w");
        while (p != NULL)
        {
            fprintf(fp, "%s\t%s\t%d \t%d\t%d\t%d\n", p->num, p->name, p->sub1, p->sub2, p->sub3, p->ave);
            p = p->next;
        }
        fclose(fp);
        printf("保存成功!\n");
        return head;
    }
    struct student* read()//从文件读取链表
    {
        struct student* p1, * p2=new student;
        FILE* fp;
        int flag = 1;
        if ((fp = fopen("stud.txt", "r")) == NULL)
            printf("文件无法打开!\n");
        else
            fp = fopen("stu.txt", "r");
        printf("学生学号 姓名 语文成绩 数学成绩 英语成绩 平均成绩:\n");
        while (!feof(fp))
        {
            if (flag == 1) ///读取头
            {
                p1 = (struct student*)malloc(sizeof(struct student));
                head = p1;
                p2 = p1;
                fscanf(fp, "%s %s %d %d %d %d\n", p1->num, p1->name, &p1->sub1, &p1->sub2, &p1->sub3, &p1->ave);
                printf("%s\t%s\t%d\t%d\t%d\t%d\n", p1->num, p1->name, p1->sub1, p1->sub2, p1->sub3, p1->ave);
                flag--;
            }
            else
            {
                p1 = (struct student*)malloc(sizeof(struct student));
                fscanf(fp, "%s %s %d %d %d %d\n", p1->num, p1->name, &p1->sub1, &p1->sub2, &p1->sub3, &p1->ave);
                printf("%s\t%s\t%d\t%d\t%d\t%d\n", p1->num, p1->name, p1->sub1, p1->sub2, p1->sub3, p1->ave);
                p2->next = p1;
                p2 = p1;
            }
        }
        p2->next = NULL;
        fclose(fp);
        return head;
    }
    int main()
    {
        creat();
        save(head);
        read();
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月11日
  • 已采纳回答 1月3日
  • 创建了问题 1月3日

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器