Yinzhien
2021-07-10 21:35
采纳率: 93.3%
浏览 28

C语言写职工管理程序多种错误,,如何成功打开并读取文件?

img
虽然读取文件了,但是好像只能显示在运行屏幕,运行选择分函数时还是空文件找不到资料

img

img

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

//职工数据结构体 
struct Worker    
{    //6个属性 
    char  num[20];             //工号
    char name[20];            //姓名  
    char inday[20];          //入厂年月 
    char  job[20];          //职称 
    char department[20];    //工作部门
    char  salary[20] ;     //工资 
}worker;



//根据数据结构体定义职工链表结构体 
typedef struct node
{
    struct Worker worker;
    struct node *next;
    struct node *prior;
}Node; 

//定义分函数 
int getMeun(Node *worker);         //获取菜单 
int addWorker(Node *worker);    //增加职工
int delWorker(Node *worker);    //删除职工
int querWorker(Node *worker);    //查询职工
int revWorker(Node *worker);    //修改职工
int saveFile (Node* worker);    //存储到文件中 
int saveNode (Node* worker);     //输出排序好的链表但不更改文件
int readFile(Node *worker);        //读取文件信息 
int next(Node* worker);            //下一步 

//主函数 
int main()
{
    Node *worker;
    worker = (Node*)malloc(sizeof(Node)); //创建头结点 
    worker->next = worker->prior =  NULL;
    system("color 3f");
    readFile(worker);    //读取文件 
    getMeun (worker);   //获取主菜单 
    return 0;
}    


int readFile(Node *worker)
{
    Node *p;
    FILE *fp;
    int i=0; 

    fp = fopen("C:\\职工资料.txt","r");
    
//    p = (Node *)malloc(sizeof(Node));
//    fscanf(fp,"%s\t%s\t%s\t%s\t%s\t%s\n",
//                    p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
    if(fp == NULL)
    {
        printf("文件打开失败,请在C盘下建立一个文本文件"); 
        return 0;
    }
    p=worker->next;
    while(p!=NULL)
    {
        
        
        fscanf(fp,"%s\t%s\t%s\t%s\t%s\t%s\n",
                    p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
                    p=p->next;
    }
/*  while(fscanf(fp,"%d",&worker[i])!=EOF)
    {
        i++; 
    } 
*/    
    fclose(fp);    
}

 

/*
//读取文件信息
int readFile(Node *worker)
{
  Node *p;
  FILE *fp;//创建文件指针
  worker=(Node *)malloc(sizeof(Node));//读取内存
  p=worker->next;
  if((fp= fopen("C:\\职工资料.txt","r")) == NULL)
    { 
        printf("文件打开失败"); 
        return 0;
    }
  hangFile(fp); 
  fclose(fp);
  return 1;
}
int hangFile(FILE *fp)
{ 
  int line=1;
  char ch;//单个人的信息字符串
  while((ch=fgetc(fp))!=EOF)//读取到文件末尾或读取失败时返回EOF
  {
    if(ch == '\n')
    {
      line++;
    }
    printf("%c",ch);
  }
  return 1;
}
*/
//存储文件信息
int saveFile (Node* worker)
{
    Node *p = NULL;
    FILE *fp;
    int saveChoose;
    fp = fopen("C:\\职工资料.txt","w"); 
        if(fp == NULL)
        {
            printf("文件无法打开\n"); 
            return 0;
        } 
        if(worker->next == NULL)
        {
            printf("没有记录\n"); 
        }
        else
        {
            p = worker->next;
        } 
        
        while(p!=NULL)
        {

            fprintf(fp,"%s\t%s\t%s\t%s\t%s\t%s\n",
                    p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
                     
            p = p->next;    
        }
        fclose(fp);

}


    
//1·主菜单
int getMeun (Node *worker)   
{
    int choose = 0 ;

    printf("**********************************欢 迎来到职工管理系统**************************************\n"); 
    printf("*                                 1. 新增职工                                              *\n");
    printf("*                                 2. 删除职工                                              *\n");
    printf("*                                 3. 查询职工                                              *\n");
    printf("*                                 4. 修改职工                                              *\n");
    printf("*                                 5. 排序职工                                              *\n");
    printf("*                                 0. 退出系统                                              *\n");
    printf("********************************************************************************************\n");
    printf("*请输入需要进行的操作所对应的数字:                                                       \n");    
    scanf("%d",&choose);
    
    
    
    switch(choose) //判断并选择进行的操作 
    {
    case 1:
        system("cls");//清屏以免界面杂乱 
        printf("**********************************************************************************************\n");
        printf("*                              欢 迎进入 新增职工 页面                                        *\n");
        printf("**********************************************************************************************\n");
        addWorker(worker);  
        saveFile(worker);
        next(worker); 
        
        break;
    
    case 2:
        system("cls");
        printf("**********************************************************************************************\n");
        printf("*                              欢 迎进入 删除职工 页面                                        *\n");
        printf("**********************************************************************************************\n");
        delWorker(worker);
        next(worker);
        break;
    case 3:
        system("cls");
        printf("**********************************************************************************************\n");
        printf("*                              欢 迎进入 查询职工 页面                                        *\n");
        printf("**********************************************************************************************\n");
        querWorker(worker);
        next(worker);
        break;
    case 4:
        system("cls");
        printf("**********************************************************************************************\n");
        printf("*                              欢 迎进入 修改职工 页面                                        *\n");
        printf("**********************************************************************************************\n");
        revWorker(worker);
        saveFile(worker);
        next(worker);
        break;
    case 0:
        exit(0);//退出系统 
    default:
        system("cls");
        getMeun(worker);  //递归 
    }
}



//2·增加职工
int addWorker (Node *worker)    
{    
    Node *p,*k;
    p=worker;    
    p = (Node *)malloc(sizeof(Node));
    k=worker->next;

         printf("请按照工号、姓名、入厂年月、职称、工作部门、工资 的顺序依次输入所添加职工的相关信息\n"); 
        printf("请输入职工工号:") ;
         scanf("%s",&p->worker.num);    
         
         while(k!=NULL)//对工号进行判断 ,循环多次判断 
         {
             if(strcmp(p->worker.num ,k->worker.num) == 0)//strcmp用于比较两个字符串并根据比较结果返回整数 
             {
                 printf("该编号已存在,请重新输入编号!\n");
                scanf("%s",p->worker.num);
                k = worker;             //指回头结点,再次遍历判断 
            }
                k = k->next;    
        }
        
        printf("请输入职工姓名:");
        scanf("%s",&p->worker.name);    
         printf("请输入职工入厂时间:");
         scanf("%s",&p->worker.inday);
         printf("请输入职工职称:");
        scanf("%s",&p->worker.job);
        printf("请输入职工工作部门:");
        scanf("%s",&p->worker.department);
        printf("请输入职工工资:");    
        scanf("%s",&p->worker.salary);
        
        p->next=NULL;
        k->next=p;
        k=p;
        saveFile(worker);
        printf("新增的职工信息如下:\n\n");
        printf("工号\t姓名\t入厂年月\t职称\t工作部门\t工资 \n"); 
        printf("%s\t%s\t%s\t%s\t%s\t%s\n",p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
        printf("职工信息已成功存入文件!\n");
        
        next(worker);
        
    
}
    
/*    while(q!=NULL)
    {
                                          //大于前结点而且小于后结点,插入于中间 
        if( (strcmp(p->worker.num,pre->worker.num) > 0 ) && ( strcmp(p->worker.num,q->worker.num) < 0 ) )
        {
            p->next = q;
            p->prior = q->prior;
            q->prior = p;
            p->prior->next = p;
            break;
        }    
        //插入于最后一个的后面,即q的后面 。尾插法 
        else if( (q->next == NULL) &&  ( strcmp(p->worker.num,pre->worker.num) > 0 ))
        {
            q->next = p;
            p->prior = q;
            p->next = NULL;
            break;
        }
       
    
    
    }    
         
    */
    



//3·删除职工
int delWorker(Node *worker)        
{
    Node *p,*r;
    char delnum[20],delname[20];
    int delChoose,confirmChoose;
    p = worker->next;
    
    if( p == NULL)
    {
        printf("空\n"); 
    }
    else{
     
    printf("按职工工号删除: 1\n");
    printf("按职工姓名删除: 2\n");
    printf("********************************************************************************************\n");
    printf("请选择你要删除的方法:");
    scanf("%d",&delChoose);
        if(delChoose == 1 ) //按工号的情况
        {
            int num=0; 
            printf("请输入要删除职工的工号:\n"); 
            scanf("%s\n",&delnum);
            while(p!=NULL)
            {    
                if(strcmp(p->worker.num,delnum)==0)
                {
                    num++;
                    printf("工号\t姓名\t入厂年月\t职称\t工作部门\t工资 \n"); 
                    printf("%s\t%s\t%s\t%s\t%s\t%s\n", 
                    p->worker.num,p->worker.name,p->worker.inday,
                    p->worker.job,p->worker.department,p->worker.salary);
                    
                    printf("您是否删除%s\n\n",p->worker.name);
                    printf("确认删除:1  取消删除:2\n");
                    printf("请输入你选择的操作数字:"); 
                    scanf("%d",&confirmChoose);
                    if(confirmChoose == 1)
                    {
                        printf("职工%s删除成功\n",p->worker.name); 
                            //删除该职工信息占据的节点 
                            if(p!=NULL)
                            {
                                r = worker;
                                while (r->next != p)//去掉p 
                                {
                                    r = r->next;    
                                }
                                r->next = r->next->next;    
                            }
                            saveFile(worker);//将其存储到文件中 
                    }
                    else if(confirmChoose == 2)//取消删除 
                    {
                        printf("您已取消操作\n"); 
                        break;
                    }
                    break;
                }
                else
                {
                    if(p->next == NULL && num == 0)
                    {
                        printf("该职工不存在\n"); 
                    }    
                }
                p = p->next;    
            }
        }//delChoose == 1 
    
        else if (delChoose == 2 )//按照姓名的情况 
        {
            printf("请输入要删除职工的姓名:\n"); 
            scanf("%s",&delname);
            int num=0; 
            while(p!=NULL)
            {    
                if(strcmp(p->worker.name,delname)==0)
                {
                    num++;
                    printf("工号\t姓名\t入厂年月\t职称\t工作部门\t工资 \n"); 
                    printf("%s\t%s\t%s\t%s\t%s\t%s\n",
                    p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
                    printf("您是否删除%s\n",p->worker.name);
                    printf("确认删除:1 取消删除:2\n");
                    printf("请输入你选择的操作数字:") ;
                    scanf("%d",&confirmChoose);
                    if(confirmChoose == 1)
                    {
                        printf("职工%s删除成功\n",p->worker.name); 
                        //删除该职工信息占据的节点 
                            if(p!=NULL)
                            {
                                r = worker;
                                while (r->next != p)
                                {
                                    r = r->next;    
                                }
                                r->next = r->next->next;    
                            }
                            saveFile(worker);
                    }
                    else if(confirmChoose == 2)
                    {
                        printf("您已取消操作\n"); 
                        break;
                    }
                    break;
                }
                else
                {
                    if(p->next == NULL && num == 0)
                    {
                        printf("该职工不存在\n"); 
                    }    
                }
                p = p->next;    
            }
        }//delChoose == 2 
    }
    

};




//4·查询职工
int querWorker(Node *worker)    
{
    char quernum[20],quername[20],querinday[20],querjob[20],querdepartment[20],quersalary[20];
    int querChoose,NumSta,NumEnd,querSalaryNum;  
    
    Node *p=worker->next;
    p = (Node *)malloc(sizeof(Node));
    readFile(worker); 
    if(p==NULL)
    {
        printf("目前没有资料\n"); 
    }
    else
    {
        printf("通过职工工号查询:         1 \n");
        printf("通过职工工作部门查询:     2 \n");
        printf("通过职工姓名查询:          3 \n");
    
        printf("********************************************************************************************\n");
        printf("请输入查所对应数字:\n");
        scanf("%d",&querChoose);
        
        if(querChoose == 1 )
        {
            int num = 0; 
            printf("请输入要查询的职工工号:\n");
            scanf("%s",&quernum);
            while(p!=NULL)
            {    
                if(strcmp(p->worker.num,quernum)==0)
                {
                    num++;
                    if(num==1)
                    {
                        printf("********************************************************************************************\n");
                        printf(" 工号\t姓名\t入厂年月\t职称\t工作部门\t工资\n"); 
                    }     
                    printf("%s\t%s\t%s\t%s\t%s\t%s\n",
                    p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
                }
                else
                {
                    if (p->next == NULL && num == 0)
                    {
                        printf("未找到该职工\n");
                    }
                }
                p = p->next;
            }    
        }// 通过职工工号查询
    
    
        
        else if (querChoose == 2 )
        {
            int num = 0 ; 
            printf("请输入要查询的职工工作部门:\n");
            scanf("%s",&querdepartment);
            while(p!=NULL)
            {    
                if(strcmp(p->worker.department,querdepartment)==0)
                {
                    num++;
                    if(num==1)
                    {
                        printf("********************************************************************************************\n");
                        printf(" 工号\t姓名\t入厂年月\t职称\t工作部门\t工资\n"); 
                    }     
                    printf("%s\t%s\t%s\t%s\t%s\t%s\n",
                    p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
                }
                else
                {
                    if (p->next == NULL && num == 0)
                    {
                        printf("未找到职工\n");
                    }
                }
                p = p->next;
            }    
        }
        
        
         
        else if (querChoose == 3 )//
        {
            int num = 0; 
            printf("请输入要查询的职工姓名:\n")    ;
            scanf("%s",&quername);
            while(p!=NULL)
            {    
                if(strcmp(p->worker.name,quername) == 0)
                {
                    num++;
                    if(num==1)
                    {
                        printf("********************************************************************************************\n");
                        printf(" 工号\t姓名\t入厂年月\t职称\t工作部门\t工资\n"); 
                    } 
                    printf("%s\t%s\t%s\t%s\t%s\t%s\n",
                    p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
                }
                else
                {
                    if (p->next == NULL && num == 0)
                    {
                        printf("未找到该职工\n");
                    }
                }
                p = p->next;
            }    
        } 
        
        

    
        else 
        {
            printf("输入出错,请重新选择\n");
            querWorker(worker);        
        }

    }         
    
}




//5·修改职工
int revWorker(Node *worker)    
{
    char revnum[20],revname[20]; 
    int revChoose ; 
    Node *p = worker->next; 
    
    if( p == NULL)
    {
        printf("目前没有资料\n"); 
    }
    else
    {
        printf("通过职工工号修改:     1 \n");
        printf("通过职工姓名修改:     2 \n");
        printf("请输入要进行操作所对应数字:\n");
        printf("********************************************************************************************\n");
        scanf("%d",&revChoose);
        if(revChoose == 1 )
        {
            printf("请输入要修改的职工编号\n");
            scanf("%s",&revnum);
            int num=0;
            while(p!=NULL)
            {
                if(strcmp(p->worker.num,revnum)==0)
                {
                    num++ ; 
                    //显示原信息 
                    printf("找到该职工,其信息如下:\n");
                        printf("工号\t姓名\t入厂年月\t职称\t工作部门\t工资 \n"); 
                        printf("%s\t%s\t%s\t%s\t%s\t%s\n",
                        p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
                    
                    //修改信息 
                    printf("\n请依次输入数据进行修改:\n"); 
                        printf("工号\t姓名\t入厂年月\t职称\t工作部门\t工资 \n"); 
                        printf("%s\t%s\t%s\t%s\t%s\t%s\n",
                        p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
                            
                    //修改后信息 
                    printf("修改职工信息如下:\n\n");
                        printf("工号\t姓名\t入厂年月\t职称\t工作部门\t工资 \n"); 
                        printf("%s\t%s\t%s\t%s\t%s\t%s\n",
                        p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
                    
                    break;
                    
                }
                else
                {
                    if (p->next == NULL && num == 0)
                    {
                        printf("未找到该职工\n");
                    }
                }
            p = p->next;     
            }
        }
        else if (revChoose ==2)
        {
            printf("请输入要修改的职工姓名\n");
            scanf("%s",&revname);
            
            while(p!=NULL)
            {
                if(strcmp(p->worker.name,revname)==0)
                {
                //显示原信息 
                printf("找到该职工,其信息如下\n");
                    printf("工号\t姓名\t入厂年月\t职称\t工作部门\t工资 \n"); 
                    printf("%s\t%s\t%s\t%s\t%s\t%s\n",
                    p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
                    
                //修改信息 
                printf("\n请依次输入数据进行修改:\n"); 
                    printf("工号\t姓名\t入厂年月\t职称\t工作部门\t工资\n");
                //    printf("%s\t",p->worker.num);
                    scanf("%s%s%s%s%s%s",
                    p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
        
                //修改后信息 
                printf("修改职工信息如下:\n\n");
                    printf(" 工号\t姓名\t入厂年月\t职称\t工作部门\t工资\n"); 
                    printf("%s\t%s\t%s\t%s\t%s\t%s\n",
                    p->worker.num,p->worker.name,p->worker.inday,p->worker.job,p->worker.department,p->worker.salary);
                
                    break;
                
                }
                else
                {
                    int num = 0 ;
                    if (p->next == NULL && num == 0)
                    {
                        printf("未找到该职工\n");
                    }
                }
            p = p->next;     
            }
        }

    }    

};




//6·下一步操作 
int next (Node* worker)
{
    printf("********************************************************************************************\n");
    printf("返回主菜单:1; 退出系统:0\n"); 
    int nextChoose;
    scanf("%d",&nextChoose);
    while(1)
    {
        if(nextChoose==1)
        {
            system("cls");
            getMeun(worker);     
        }
        else if (nextChoose==0)
        {
            exit(0);    
        } 
        else
        {
            printf("输入出错,请重新输入!");
            scanf("%d",&nextChoose);
            continue; 
        }    
    }
} 


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

  • CSDN专家-sinJack 2021-07-10 21:36
    最佳回答

    里面有内容吗,你读取不对吧,你读取后直接输出看看。

    评论
    解决 无用
    打赏 举报
查看更多回答(3条)