不知道怎样将文件的内容输出到链表中? 自己也写了有代码,运行的时候出错。

[复制链接]
查看11 | 回复3 | 2011-6-13 11:12:36 | 显示全部楼层 |阅读模式
#includestdio.h
#includestring.h
#includemalloc.h

structDate{
        intYear;
        intMonth;
        intDay;
};

structScore{
floatA;
floatB;
floatC;
floatALL;
};

structstudent{
intnum;
charname[20];
charsex;
structDateBirth;
structScoreS;
structstudent*next;
};

structstudentsave(structstudent*head)
{
        structstudent*p1,*p2;
        p1=p2=head;
        FILE*fp;
        if((fp=fopen(\"student_list.txt\",\"w\"))==NULL)
        printf(\"cannotopenfile\\n\");
        while(p2-next!=NULL)
        {
        fprintf(fp,\"%d%s%c%d%d%d%f%f%f\",p1-num,p1-name,p1-sex,p1-Birth.Year,p1-Birth.Month,p1-Birth.Day,p1-S.A,p1-S.B,p1-S.C);
        p2=p1;
        p1=p2-next;
        }
        printf(\"数据写入完成!\");
        fclose(fp);
        return*head;
}

structstudentoutput(structstudent*head)
{
        head=NULL;
        FILE*fp;
        if((fp=fopen(\"student_list.txt\",\"r\"))==NULL)
        {printf(\"cannotopenfile\\n\");return*head;}
        structstudent*p1,*p2;
        p1=p2=(structstudent*)malloc(sizeof(structstudent));
        fread(p1,sizeof(structstudent),1,fp);
        while(!feof(fp))
        {
                if(head==NULL)head=p1;
                elsep2-next=p1;
                p2=p1;
                p1=(structstudent*)malloc(sizeof(structstudent));
                fread(p1,sizeof(structstudent),1,fp);

        }
        p2-next=NULL;
fclose(fp);
        return*head;
}

//creatnew()函数,创建新的学生信息库;
structstudentcreatnew(structstudent*head)
{
        structstudent*p1,*p2;
        head=NULL;
        p1=(structstudent*)malloc(sizeof(structstudent));
        input(p1);sum=1;
        while(p1-num!=0)
        {
                if(head==NULL)head=p1;
                elsep2-next=p1;
                p2=p1;
                p1=(structstudent*)malloc(sizeof(structstudent));
                input(p1);sum=sum1;
        }
        p2-next=NULL;
        save(head);
        return*head;
}
回复

使用道具 举报

千问 | 2011-6-13 11:12:36 | 显示全部楼层
你的output改为如下试试:
structstudentoutput(structstudent*head)
{
        head=NULL;
        FILE*fp;
        if((fp=fopen(\"student_list.txt\",\"r\"))==NULL)
        {printf(\"cannotopenfile\\n\");return*head;}
        structstudent*p1,*p2;
        head=p2=(structstudent*)malloc(sizeof(structstudent));
        //fread(p1,sizeof(structstudent),1,fp);
        while(!feof(fp))
        {
p1=(structstudent*)malloc(sizeof(structstudent));
                fread(p1,sizeof(structstudent),1,fp);

                if(head==NULL)head=p1;
                elsep2-next=p1;
                p2=p1;
                //p1=(structstudent*)malloc(sizeof(structstudent));
                //fread(p1,sizeof(structstudent),1,fp);

        }
        p2-next=NULL;
fclose(fp);
        return*head;
}









<h4class=\"ask\">追问


谢了不过还是不能用
回复

使用道具 举报

千问 | 2011-6-13 11:12:36 | 显示全部楼层
我是试着打看有没有用的刚开始我是用fscanf()的也不能用
回复

使用道具 举报

千问 | 2011-6-13 11:12:36 | 显示全部楼层
................不知道你在说什么?????我要实现的不是这个功能
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行