| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 789 人关注过本帖
标题:求大神,本人多次调试无效,怀疑是count()函数的问题,fp未初始化?
只看楼主 加入收藏
woaiwojia123
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-12-23
收藏
 问题点数:0 回复次数:1 
求大神,本人多次调试无效,怀疑是count()函数的问题,fp未初始化?
#include<stdio.h>
#include<string.h>
#define N 200
struct employee
{
    int  num;
    char name[20];
    char sex;
    char marital[10];
    char xuewei[20];
    char bumen[20];
    char zhiwei[20];
    int  wage;
    int  gonglin;
} em[N];
save(int n)
{  
 int i;
 FILE*fp;  
 fp=fopen("em.txt","wb");
 for(i=0;i<n;i++)
     fwrite(&em[i],sizeof(struct employee),1,fp);
 fclose(fp);
}
count()
{
 FILE*fp;  
 int i;
 for(i=0;!feof(fp);i++)
     fread(&em[i],sizeof(struct employee),i,fp);
 fclose(fp);
 return(i-1);
}




void creat()
{
    int i,n;
    printf("请输入职工人数(1-200):");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("请输入职工号:");
        scanf("%d",&em[i].num);
        printf("请输入职工姓名:");
        scanf("%s",em[i].name);
        printf("请输入职工性别:");
        scanf("%s",&em[i].sex);
        printf("请输入职工婚姻状况:");
        scanf("%s",em[i].marital);
        printf("请输入职工学位:");
        scanf("%s",em[i].xuewei);
        printf("请输入职工部门:");
        scanf("%s",em[i].bumen);
        printf("请输入职工职位:");
        scanf("%s",em[i].zhiwei);
        printf("请输入职工工资:");
        scanf("%d",&em[i].wage);
        printf("请输入职工工龄:");
        scanf("%d",&em[i].gonglin);
        printf("\n");
    }
    printf("\n职工信息创建完毕!\n");
    save(n);
}
void modify()  
{
    int i,n,k=-1,num;
    n=count();
    printf("请输入要修改数据的职工号: ");
    scanf("%d",&num);
    for(i=0;i<n;i++)
        if(num=em[i].num)
        {k=i;break;}
    if(k==-1)printf("该职工号不存在\n");
    else
    {
         printf("  职工号\t姓名\t性别\t婚姻状况\n");
         printf("\n  %d\t%s\t%c\t%s\t\n",em[i].num,em[i].name,em[i].sex,em[i].marital);   
         printf("\n  学位\t部门\t职位\t工资\t工龄   \n ");
         printf("\n  %s\t%s\t%s\t%d\t%d\n",em[i].xuewei,em[i].bumen,em[i].zhiwei,em[i].wage,em[i].gonglin);
         printf("\n");
         printf("\n请重新输入该职工的数据:\n");
         printf("请输入职工号:");
         scanf("%d",&em[i].num);
         printf("请输入职工姓名:");
         scanf("%s",em[i].name);
         printf("请输入职工性别:");
         scanf("%c",&em[i].sex);
         printf("请输入职工婚姻状况:");
         scanf("%s",em[i].marital);
         printf("请输入职工学位:");
         scanf("%s",em[i].xuewei);
         printf("请输入职工部门:");
         scanf("%s",em[i].bumen);
         printf("请输入职工职位:");
         scanf("%s",em[i].zhiwei);
         printf("请输入职工工资:");
         scanf("%d",&em[i].wage);
         printf("请输入职工工龄:");
         scanf("%d",&em[i].gonglin);
         printf("\n");
    }
    save(n);
}
void display()
{
    int i;
    int n;
        n=count();
    printf("  职工号\t姓名\t性别\t婚姻状况   \n");
    for(i=0;i<n;i++)
    {
        printf("\n  %d\t%s\t%c\t%s\n",em[i].num,em[i].name,em[i].sex,em[i],em[i].marital);
        printf("\n  学位\t部门\t职位\t工资\t工龄   \n ");
        for(i=0;i<n;i++)
        printf("\n  %s\t%s\t%d\t%d\n",em[i].xuewei,em[i].bumen,em[i].zhiwei,em[i].wage,em[i].gonglin);
    }
}
void search()
{  
    int i,n,k=-1;
    int num;
        n=count();
    printf("\n请输入要查找的职工号:");
    scanf("%d",&num);   
    for(i=0;i<n;i++)
        if(num==em[i].num)
        { k=i;break; }
     if(k==-1)printf("员工的数据不存在\n");
     else
     {
         printf("  职工号\t姓名\t性别\t婚姻状况\n");     
         printf("\n  %d\t%s\t%c\t%s\t\n",em[i].num,em[i].name,em[i].sex,em[i].marital);   
         printf("\n  学位\t部门\t职位\t工资\t工龄   \n ");
         printf("\n  %s\t%s\t%d\t%d\n",em[i].xuewei,em[i].bumen,em[i].zhiwei,em[i].wage,em[i].gonglin);
     }
 }
void del()
{
    int i,k=-1,n;
    int num;
        n=count();
    printf("请输入要删除的职工号:");
    scanf("%d",&num);
    for(i=0;i<n;i++)
        if(num=em[i].num)
        {k=i;break;}
        if(k==-1)
    printf("数据不存在\n");
        else
        {
            printf("职工号\t姓名\t性别\t婚姻状况\n");
            printf("%d\t%s\t%s\t%s\n",em[k].num,em[k].name,em[k].sex,em[k].marital);
            printf("部门\t职位\t工资\t工龄\n");
            printf("%s\t%s\t%s\t%s\n",em[k].bumen,em[k].zhiwei,em[k].wage,em[k].gonglin);
        for(i=k;i<n-1;i++)
            em[i]=em[i+1];
        n=n-1;
        save(n);
        }        
}
void showmenu()
{
    printf("\n     职工信息管理系统    \n");
    printf("\n********菜单选项*********\n");
    printf("  1.增加员工信息           \n");
    printf("  2.修改员工信息           \n");
    printf("  3.显示员工信息           \n");
    printf("  4.查询员工信息           \n");
    printf("  5.删除员工信息           \n");
    printf("  6.保存员工信息至记录文件 \n");
    printf("  7.从记录文件读取文件信息 \n");
    printf("  8.退出                   \n");
    printf("\n*************************\n");
    printf("\n请输入你的选择(0——8):");
}
main()
{
    int choice;
    showmenu();
    scanf("%d",&choice);
    while (choice!=0)
    {
        switch(choice)
        {
             case 1:creat();break;
             case 2:modify();break;
             case 3:display();break;
             case 4:search();break;
             case 5:del();break;
             case 6:break;
             case 7:break;
             case 8:break;

        }
        showmenu();
        scanf("%d",&choice);
    }
}
搜索更多相关主题的帖子: employee include count 
2014-12-23 18:32
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:0 
main.c:16:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]

save(int n)
^
main.c:24:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
main.c:25:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]

count()
^
main.c:75:15: warning: using the result of an assignment as a condition without
      parentheses [-Wparentheses]
        if(num=em[i].num)
           ~~~^~~~~~~~~~
main.c:75:15: note: place parentheses around the assignment to silence this
      warning
        if(num=em[i].num)
              ^
           (            )
main.c:75:15: note: use '==' to turn this assignment into an equality comparison

        if(num=em[i].num)
              ^
              ==
main.c:116:70: warning: format specifies type 'char *' but the argument has type

      'struct employee' [-Wformat]
        printf("\n  %d\t%s\t%c\t%s\n",em[i].num,em[i].name,em[i].sex,em[i],em[i]
...
                                ~~                                   ^~~~~
main.c:116:76: warning: data argument not used by format string
      [-Wformat-extra-args]
        printf("\n  %d\t%s\t%c\t%s\n",em[i].num,em[i].name,em[i].sex,em[i],em[i]
...
               ~~~~~~~~~~~~~~~~~~~~~~                                      ^
main.c:119:64: warning: format specifies type 'int' but the argument has type
      'char *' [-Wformat]
        printf("\n  %s\t%s\t%d\t%d\n",em[i].xuewei,em[i].bumen,em[i].zhiwei,em[i
...
                            ~~                                 ^~~~~~~~~~~~
                            %s
main.c:119:88: warning: data argument not used by format string
      [-Wformat-extra-args]
  ..."\n  %s\t%s\t%d\t%d\n",em[i].xuewei,em[i].bumen,em[i].zhiwei,em[i].wage,e..
.
     ~~~~~~~~~~~~~~~~~~~~~~                                                  ^
main.c:138:65: warning: format specifies type 'int' but the argument has type
      'char *' [-Wformat]
  ...printf("\n  %s\t%s\t%d\t%d\n",em[i].xuewei,em[i].bumen,em[i].zhiwei,em[i...

                         ~~                                 ^~~~~~~~~~~~
                         %s
main.c:138:89: warning: data argument not used by format string
      [-Wformat-extra-args]
  ..."\n  %s\t%s\t%d\t%d\n",em[i].xuewei,em[i].bumen,em[i].zhiwei,em[i].wage,e..
.
     ~~~~~~~~~~~~~~~~~~~~~~                                                  ^
main.c:149:15: warning: using the result of an assignment as a condition without

      parentheses [-Wparentheses]
        if(num=em[i].num)
           ~~~^~~~~~~~~~
main.c:149:15: note: place parentheses around the assignment to silence this
      warning
        if(num=em[i].num)
              ^
           (            )
main.c:149:15: note: use '==' to turn this assignment into an equality
      comparison
        if(num=em[i].num)
              ^
              ==
main.c:156:60: warning: format specifies type 'char *' but the argument has type

      'char' [-Wformat]
            printf("%d\t%s\t%s\t%s\n",em[k].num,em[k].name,em[k].sex,em[...
                            ~~                             ^~~~~~~~~
                            %c
main.c:158:64: warning: format specifies type 'char *' but the argument has type

      'int' [-Wformat]
  ...printf("%s\t%s\t%s\t%s\n",em[k].bumen,em[k].zhiwei,em[k].wage,em[k].gong...

                     ~~                                 ^~~~~~~~~~
                     %d
main.c:158:75: warning: format specifies type 'char *' but the argument has type

      'int' [-Wformat]
  ...%s\n",em[k].bumen,em[k].zhiwei,em[k].wage,em[k].gonglin);
     ~~                                        ^~~~~~~~~~~~~
     %d
main.c:180:1: warning: type specifier missing, defaults to 'int'
      [-Wimplicit-int]
main()
^
15 warnings generated.

Only the Code Tells the Truth             K.I.S.S
2014-12-23 18:53
快速回复:求大神,本人多次调试无效,怀疑是count()函数的问题,fp未初始化?
数据加载中...
 
   



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

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