| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 322 人关注过本帖
标题:从文件中读取内容到链表有问题,求大神指点
只看楼主 加入收藏
好听个鬼子
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-10-30
收藏
 问题点数:0 回复次数:0 
从文件中读取内容到链表有问题,求大神指点
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define name_max 10

typedef struct reader
{
    char name[name_max];
    long number;
    int sex;
    int classnumber;
    int state;
    struct reader *next;
}REA;

int menu();
REA *Initlist(REA *head);
REA *Display();
void Delete(REA *head);

int main()
{
    int choose;
    int flag=1;   
    REA *head=NULL;

    while(flag)
    {
        choose=menu();

        switch(choose)
        {
        case 1:
            head=Initlist(head);
            system("pause");
            system("cls");
            break;
        case 2:
            Delete(head);
            head=Display();
            system("pause");
            system("cls");
            break;
        case 3:
            Delete(head);
            flag=0;
            break;
        default:
            printf("wrong\n");
            system("pause");
            system("cls");
        }
    }

    return 0;
}

int menu()
{
    int choose,ret;

    printf("**********************************************************************\n");
    printf("*                   library system                                   *\n");
    printf("*      1 to initlist                                                 *\n");
    printf("*      2 to display                                                  *\n");
    printf("*      3 to exit                                                     *\n");
    printf("**********************************************************************\n");

    do
    {   
        printf("please choose:");
        ret=scanf("%d",&choose);

        if(!ret)
        {
            fflush(stdin);
        }
    }while(!ret);

    return choose;
}

REA *Initlist(REA *head)
{
    FILE *fp;
    REA *p=head,*s=NULL;
    char name[name_max];
    long number;
    int sex;
    int classnumber;
    int state;

    printf("please input students' name:\n");
    scanf("%10s",name);
    printf("please input students' number:\n");
    scanf("%10ld",&number);
    printf("please input students' sex:\n");
    scanf("%d",&sex);
    printf("please input students' classnumber:\n");
    scanf("%d",&classnumber);
    printf("please input students' state:\n");
    scanf("%d",&state);

    s=(REA *)malloc(sizeof(REA));
    if(s==NULL)
    {
        printf("failure to creat line!\n");
        exit(1);
    }

    if(head==NULL)
    {
        head=s;
    }
    else
    {
        while(p->next!=NULL)
        {
            p=p->next;
        }
        p->next=s;
    }

    strcpy(s->name,name);
    s->number=number;
    s->sex=sex;
    s->classnumber=classnumber;
    s->state=state;
    s->next=NULL;

    if((fp=fopen("E:\\library.txt","a"))==NULL)
    {
        printf("failure to open file\n");
        exit(0);
    }

    fwrite(s,sizeof(REA),1,fp);
    fclose(fp);

    return head;
}  

REA *Display()
{
    FILE *fp;
    REA *head=NULL,*s=NULL,*p=NULL;

    if((fp=fopen("E:\\library.txt","r"))==NULL)
    {
        printf("failure to open file\n");
        exit(0);
    }

    while(!feof(fp))
    {
        s=(REA *)malloc(sizeof(REA));
        if(s==NULL)
        {
            printf("failure to creat line!\n");
            exit(1);
        }

        fread(s,sizeof(REA),1,fp);
        printf("name:%10s\n",s->name);
        printf("number:%10ld\n",s->number);
        printf("sex:%d\n",s->sex);
        printf("classnumber:%d\n",s->classnumber);
        printf("state:%d\n",s->state);

        if(head==NULL)
        {
            head=s;
            p=s;
        }
        else
        {
            p->next=s;
            p=s;
        }
    }

    fclose(fp);

    return head;
}

void Delete(REA *head)
{
    REA *p=head,*s=NULL;

    while(p!=NULL)
    {
        s=p;
        p=p->next;
        free(s);
    }
}
搜索更多相关主题的帖子: include number 
2015-03-19 15:01
快速回复:从文件中读取内容到链表有问题,求大神指点
数据加载中...
 
   



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

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