| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 453 人关注过本帖
标题:文件读取和存储链表数据问题
取消只看楼主 加入收藏
裕闷
Rank: 1
来 自:广州
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-10-16
收藏
 问题点数:0 回复次数:0 
文件读取和存储链表数据问题
职工管理系统的作业,需要将链表数据从文件中读取和保存。请大神帮忙看看,读取和保存函数错在哪~~谢谢
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct staff
{
    int    number;
    char   name[10];
    char   sex[3];
    int    age;
    char   education[10];
    int    wage;
    char   adress[40];
    char   phone[15];
    struct staff *next;
}staff;
struct staff *sta_first,*sta_end;
char gasave,gfirst;
int i=1;
int add();
int find();
int dele();
void exit();
void show();
int modify();
int select();
void welcome();
void readdata();
void savedata();
struct staff* findedu(char *name);
struct staff* findwage(int num);
void displaysta(struct staff *sta,char *field,char *name);

void welcome()
{
    printf("* * * * * * * * * * * * * * * * * * * * * *\n");
    printf(">>>>>>>>>>>欢迎进入职工管理系统<<<<<<<<<<<<\n");
    printf("* * * * * * * * * * * * * * * * * * * * * *\n");
    printf("*                                         *\n");
    printf("*            职工信息管理系统             *\n");
    printf("*                                         *\n");
    printf("*          1.输入职工信息                 *\n");
    printf("*          2.查询职工信息                 *\n");
    printf("*          3.修改职工信息                 *\n");
    printf("*          4.删除职工信息                 *\n");
    printf("*          5.浏览职工信息                 *\n");
    printf("*          0.退出系统                     *\n");
    printf("*                                         *\n");
    printf("* * * * * * * * * * * * * * * * * * * * * *\n");
    printf(">>>>>>>>>>>>请选择您需要的操作!<<<<<<<<<<<<\n");
    printf("* * * * * * * * * * * * * * * * * * * * * *\n");
}

int add()
{
    FILE   *fp;
    struct staff  *sta;
    int    i=0;
    char   choice='y';
    if((fp=fopen("E:\\staff.txt","ab"))==NULL)
    {
        printf("打开文件staff.txt出错!\n");
        getchar();
        return-1;
    }
    do{
        sta=(struct staff*)malloc(sizeof(struct staff));
        if(sta==NULL)
        {
            printf("内存分配失败,按任意键退出!\n");
            getchar();
            return-1;
        }
        printf("请输入职工的信息~\n");
        printf("___________________________\n");
        printf("工号: ");
        scanf("%d",&sta->number);
        printf("姓名: ");
        scanf("%s",&sta->name);
        printf("性别: ");
        scanf("%s",&sta->sex);
        printf("年龄: ");
        scanf("%d",&sta->age);
        printf("学历: ");
        scanf("%s",&sta->education);
        printf("工资: ");
        scanf("%d",&sta->wage);
        printf("住址: ");
        scanf("%s",&sta->adress);
        printf("电话: ");
        scanf("%s",&sta->phone);
        sta->next=NULL;
        if(sta_first==NULL)
        {
            sta_first=sta;
            sta_end=sta;
        }
        else
        {
            sta_end->next=sta;
            sta_end=sta;
        }
        fwrite(sta_end,sizeof(staff),1,fp);
        gfirst=0;
        printf("\n");
        printf("___________________________\n");
        printf("\n是否继续输入?(y/n)");
        fflush(stdin);
        choice=getchar();
        if(choice!='y')
        {
            fclose(fp);
            printf("\n输入完毕,按任意键返回\n");
            system("pause");
            return-1;
        }
        system("cls");
    }while(1);
}

int dele()
{
    int findok=0;
    struct staff *sta1,*sta2;
    char name[10],choice;
    system("cls");
    printf("\n请输入要删除的员工姓名: ");
    scanf("%s",name);
    sta1=sta_first;
    sta2=sta1;
    while(sta1)
    {
        if(strcmp(sta1->name,name)==0)
        {
            findok=1;
            system("cls");
            printf("员工: %s的信息如下:\n",sta1->name);
            printf("___________________________\n");
            printf("员工: %d\n",sta1->number);
            printf("姓名: %s\n",sta1->name);
            printf("性别: %s\n",sta1->sex);
            printf("年龄: %d\n",sta1->age);
            printf("学历: %s\n",sta1->education);
            printf("工资: %d\n",sta1->wage);
            printf("住址: %s\n",sta1->adress);
            printf("电话: %s\n",sta1->phone);
            printf("___________________________\n");
            printf("您真的要删除该员工吗?(y/n)");
            fflush(stdin);
            choice=getchar();
            if(choice!='y'&&choice!='Y')return-1;
            if(sta1==sta_first) sta_first=sta1->next;
            else sta2->next=sta1->next;
            free(sta1);
            gasave=1;
            return-1;
        }  
        else
        {
            sta2=sta1;
            sta1=sta1->next;
        }
    }
    if(!findok)
    {
        printf("___________________________\n");
        printf("\n没有找到姓名是: %s的信息!\n",name);
        system("pause");
    }
    return-1;
}

int find()
{
    int choice,ret=0,num;
    char str[13];
    struct staff *sta1;
    system("cls");
    do{
        printf("\t查询职工信息\n");
        printf("___________________________\n");
        printf("\t1.按工资查询\n");
        printf("\t2.按学历查询\n");
        printf("\t0.返回主菜单\n");
        printf("___________________________\n");
        printf("\n请选择菜单: ");
        do{
            fflush(stdin);
            choice=getchar();
            switch(choice)
            {
                case'1':
                    printf("\n输入要查询的职工工资: ");
                     scanf("%d",&num);
                    sta1=findwage(num);
                    itoa(num,str,10);
                    displaysta(sta1,"工资",str);
                    break;
                case'2':
                    printf("\n输入要查询的职工学历: ");
                    scanf("%s",str);
                       sta1=findedu(str);
                    displaysta(sta1,"学历",str);
                    break;
                case'0':
                    ret=1;
                    break;
            }
          }while(choice<'0'||choice>'2');
        if(ret) break;
    }while(1);
     return-1;
}

struct staff *findwage(int num)
{
    struct staff *sta1;
    sta1=sta_first;
    while(sta1)
    {
        if(num==sta1->wage)return sta1;
        sta1=sta1->next;
    }
    return NULL;
}

struct staff *findedu(char *name)
{
    struct staff *sta1;
    sta1=sta_first;
    while(sta1)
    {
        if(strcmp(name,sta1->education)==0)return sta1;
        sta1=sta1->next;
    }
    return NULL;
}

int modify()
{   int id;
    struct staff *q;
    struct staff *h = NULL;
    printf("请输入职工号(数字):");
    scanf("%d",&id);
    h=sta_first;
    while(h)
    {
        if(h->number==id)
        {
            break;
        }
        h=h->next;
    }
    if(h == NULL)
    {
        printf("找不到此职工,请重试\n");
        system("pause");
        return-1;
    }
    printf("___________________________\n");
    printf("请输入职工姓名:");
    scanf("%s",&h->name);
    printf("请输入职工性别:");
    scanf("%s",&h->sex);
    printf("请输入职工年龄:");
    scanf("%d",&h->age);
    printf("请输入职工学历:");
    scanf("%s",&h->education);
    printf("请输入职工工资:");
    scanf("%d",&h->wage);
    printf("请输入职工住址:");
    scanf("%s",&h->adress);
    printf("请输入职工电话:");
    scanf("%s",&h->phone);
    q=sta_first;
    while(q)
    {
        if(h->number==id)
        {
            break;
        }
        q=q->next;
    }
    q=h;
    printf(".^_^.修改成功!.^_^.\n");
    system("pause");
    return-1;
}

void displaysta(struct staff *sta,char *field,char *name)
{
    if(sta)
    {
        printf("\n%s:%s信息如下: \n",field,name);
        printf("___________________________\n");
        printf("员工: %d\n",sta->number);
        printf("姓名: %s\n",sta->name);
        printf("性别: %s\n",sta->sex);
        printf("年龄: %d\n",sta->age);
        printf("学历: %s\n",sta->education);
        printf("工资: %d\n",sta->wage);
        printf("住址: %s\n",sta->adress);
        printf("电话: %s\n",sta->phone);
        printf("___________________________\n");
    }
    else
    {
        printf("___________________________\n");
        printf("资料库中没有%s为: %s的职工!请重新确认!\n",field,name);
        system("pause");
    }
}

int select()
{
    int choice;
    choice=getchar();
        switch(choice)
        {
        case'1':add();break;
        case'2':find();break;
        case'3':modify();break;
        case'4':dele();break;
        case'5':show();break;
        case'0':system("cls");printf("欢迎使用!\n");
                i=0;
                break;
        default:return -1;
        }
     return-1;
}

void readdata()
{
    FILE *fp;
    if((fp=fopen("E:\\staff.txt","rb"))==NULL)
    {
        printf("无法打开文件!\n");
        return;
    }
    struct staff *p,*q;
    p=(struct staff*)malloc(sizeof(struct staff));
    sta_first=p;
    sta_first->next=NULL;
    while(!feof(fp))
    {
        q=(struct staff*)malloc(sizeof(struct staff));
        fscanf(fp,"%d",&p->number);
        fscanf(fp,"%s",&p->name);
        fscanf(fp,"%s",&p->sex);
        fscanf(fp,"%d",&p->age);
        fscanf(fp,"%s",&p->education);
        fscanf(fp,"%d",&p->wage);
        fscanf(fp,"%s",&p->adress);
        fscanf(fp,"%s",&p->phone);
        p->next=q;
        p=q;
    }
    q->next=NULL;
    if(fclose(fp))
    {
        printf("文件异常关闭");
        return;
    }
}

void savedata()
{
    FILE *fp;
    if((fp=fopen("E:\\staff.txt","ab"))==NULL)
    {
        printf("无法打开文件!\n");
        return;
    }
    struct staff *p;
    p=sta_first->next;
    while(p!=NULL)
    {
        fprintf(fp,"%d",p->number);
        fprintf(fp,"%s",p->name);
        fprintf(fp,"%s",p->sex);
        fprintf(fp,"%d",p->age);
        fprintf(fp,"%s",p->education);
        fprintf(fp,"%d",p->wage);
        fprintf(fp,"%s",p->adress);
        fprintf(fp,"%s",p->phone);
        p=p->next;
    }
    if(fclose(fp))
    {
        printf("文件异常关闭!");
        return;
    }
}


void show()  
{  
    struct staff *sta;
    sta=sta_first;
    printf("浏览职工信息\n");
    while(sta->next!=NULL)  
    { 
        printf("___________________________\n");
        printf("员工: %d\n",sta->number);
        printf("姓名: %s\n",sta->name);
        printf("性别: %s\n",sta->sex);
        printf("年龄: %d\n",sta->age);
        printf("学历: %s\n",sta->education);
        printf("工资: %d\n",sta->wage);
        printf("住址: %s\n",sta->adress);
        printf("电话: %s\n",sta->phone);
        printf("___________________________\n");
        sta=sta->next;
    }  
    printf("显示完毕!\n");
    system("pause");
}  


int main()
{
    sta_first=NULL;
    sta_end=NULL;
    gfirst=0;
    readdata();
    for(;i==1;)
    {
    system("cls");
    welcome();
    select();
    }
    return 0;
}
搜索更多相关主题的帖子: 管理系统 number color 
2013-06-25 10:31
快速回复:文件读取和存储链表数据问题
数据加载中...
 
   



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

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