| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1058 人关注过本帖
标题:链表读取不了文件数据,求大神帮助...
只看楼主 加入收藏
慕斯蛋挞呀
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2018-2-25
收藏
 问题点数:0 回复次数:1 
链表读取不了文件数据,求大神帮助...
写的是学生成绩管理系统,只写了一部分,但是13. 从文件读取信息并显示怎么也实现不了,求大神指点。
文件里是:
4 3
    201701  刘德华  60  70  80
    201702  郭富城  80  90 100
    201703  张学友  70  80  90
    201704    黎明  75  75  75

程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 30
#define M 6

typedef struct student
{
    long studentID;
    char studentName[10];
    int score[M];
    struct student *next;
}STUDENT;

STUDENT *head;
int stunumber,subnumber;

int Menu(void);//显示菜单
void Creat(STUDENT *head,int *stunumber,int *subnumber);//创建链表,输入信息
void Write(int stunumber,int subnumber);//把学生信息写入文件
void Read(int *stunumber,int *subnumber);//读取文件

int main(void)
{
    int flag;
    do
    {
        flag=Menu();
        switch(flag)
        {
            case 1:Creat(head,&stunumber,&subnumber);break;
            case 12:Write(stunumber,subnumber);break;
            case 13:Read(&stunumber,&subnumber);break;
        }
    }while(flag);
    return 0;
}

int Menu(void)//显示菜单
{
    int flag;
    printf("1.  Input record\n");
    printf("2.  Caculate total and average score of every course\n");
    printf("3.  Caculate total and average score of every student\n");
    printf("4.  Sort in descending order by total score of every student\n");
    printf("5.  Sort in ascending order by total score of every student\n");
    printf("6.  Sort in ascending order by number\n");
    printf("7.  Sort in dictionary order by name\n");
    printf("8.  Search by number\n");
    printf("9.  Search by name\n");
    printf("10. Statistic analysis for every course\n");
    printf("11. List record\n");
    printf("12. Write to a file\n");
    printf("13. Read from a file\n");
    printf("0.  Exit\n");
    printf("Please enter your choice:");
    scanf("%d",&flag);
    printf("\n");
    return flag;
}

void Creat(STUDENT *head,int *stunumber,int *subnumber)//创建链表,输入信息
{
    int i,j;
    int flag=0;
    STUDENT *stu,*pstu;
    printf("Please enter student's number and subject's number:");
    do
    {
        flag=0;
        scanf("%d %d",stunumber,subnumber);
        if(*stunumber<=0||*stunumber>30||*subnumber<=0||*subnumber>6)
        {
            printf("Input error!Please input again.\n");
            flag=1;
        }
    }while(flag);
    stu=(STUDENT *)malloc(sizeof(STUDENT));
    head=stu;
    if(stu==NULL)
    {
        printf("Failed.\n");
        exit(0);
    }
    else
    {
        printf("No.1 student,Input ID:");
        scanf("%ld",&stu->studentID);
        printf("Input name:");
        scanf("%s",stu->studentName);
        for(j=0;j<*subnumber;j++)
        {
            printf("Input subject %d:",j+1);
            scanf("%d",&stu->score[j]);
        }
        pstu=stu;
    }
    for(i=1;i<*stunumber;i++)
    {
        stu=(STUDENT *)malloc(sizeof(STUDENT));
        if(stu==NULL)
        {
            printf("Failed.\n");
            exit(0);
        }
        else
        {
            printf("No.%d student,Input ID:",i+1);
            scanf("%ld",&stu->studentID);
            printf("Input name:");
            scanf("%s",stu->studentName);
            for(j=0;j<*subnumber;j++)
            {
                printf("Input subject %d:",j+1);
                scanf("%d",&stu->score[j]);
            }
            pstu->next=stu;
            pstu=stu;
        }
    }
    pstu->next=NULL;
}

void Write(int stunumber,int subnumber)//把学生信息写入文件
{
    int j;
    STUDENT *pstu=head;
    FILE *fp;
    if((fp=fopen("student.txt","w"))==NULL)
    {
        printf("Failure to open student.txt!\n");
        exit(0);
    }
    fprintf(fp,"%d %d\n",stunumber,subnumber);//写入学生人数和课程数
    while(pstu)
    {
        fprintf(fp,"%10ld%8s",pstu->studentID,pstu->studentName);
        for(j=0;j<subnumber;j++)
        {
            fprintf(fp,"%4d",pstu->score[j]);
        }
        fprintf(fp,"\n");
        pstu=pstu->next;
    }
    fclose(fp);
}

void Read(int *stunumber,int *subnumber)//读取文件
{
    int j;
    STUDENT *pstu=head;
    FILE *fp;
    if((fp=fopen("student.txt","r"))==NULL)
    {
        printf("Failure to open student.txt!\n");
        exit(0);
    }
    fscanf(fp,"%d %d",stunumber,subnumber);//读取学生人数和课程数
    while(pstu)
    {
        fscanf(fp,"%10ld%8s",&pstu->studentID,pstu->studentName);
        printf("%10ld%8s",pstu->studentID,pstu->studentName);
        for(j=0;j<*subnumber;j++)
        {
            fscanf(fp,"%4d",&pstu->score[j]);
            printf("%4d",pstu->score[j]);
        }
        printf("\n");
        pstu=pstu->next;
    }
    printf("\n");
    fclose(fp);
}
搜索更多相关主题的帖子: student int score flag printf 
2018-02-28 14:31
慕斯蛋挞呀
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2018-2-25
收藏
得分:0 
问题自行解决,主要原因是偷懒把创建链表和输入信息写到一起了,而head指向头结点的代码在这个函数里,所以必须先运行这个函数head指针才有效。
事实证明写代码还是老老实实的写不要偷懒的好,我可想了好长时间才找出这个问题...
2018-02-28 15:14
快速回复:链表读取不了文件数据,求大神帮助...
数据加载中...
 
   



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

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