标题:求C高手,帮忙解答一下!!万分感谢~
取消只看楼主
_下一站521
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2011-12-21
结帖率:100%
已结贴  问题点数:10 回复次数:1 
求C高手,帮忙解答一下!!万分感谢~
for (i = 0; fread(&emp[i],sizeof(struct employee),1,fp) == 1 ; i++)
是什么意思啊?
搜索更多相关主题的帖子: employee 
2012-09-03 21:02
_下一站521
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2011-12-21
得分:0 
回复 2楼 zklhp
这是一个添加函数中的一句,运行结果正确,应该没有错……
原程序如下:
void add()                        
              {
                       FILE *fp;          int i=0;
                         if ((fp = fopen("number.dat", "rb+")) == NULL)
                         {
                                    printf("Can't open file\n");
                                    exit(0);
                         }
                           fread(&number,sizeof(int),1,fp);//从文件指针位置读取
                           fclose(fp);
                         if ((fp = fopen("employee.dat", "rb+")) == NULL)
                         {
                                    printf("Can't open file\n");
                                    exit(0);
                         }
                            fread(emp,sizeof(struct employee),number,fp);
                           fclose(fp);
                         if ((fp = fopen("employee.dat", "wb+")) == NULL)
                         {
                                     printf("Can't open file\n");
                                     exit(0);
                         }
                          printf("      编号   姓名   性别   出生日期          职称          工作年限\n");
                          scanf("       %s    %s    %s    %s          %s          %d",emp[number].num,emp[number].name,emp[number].sex,emp[number].shengri,emp[number].degree,&emp[number].nianxian);
                                  number++;
                                  sort();
                          fwrite(emp,sizeof(struct employee),number, fp);
                                    fclose(fp);

                           if ((fp = fopen("number.dat", "wb+")) == NULL)
                           {
                               printf("Can't open file\n");
                               exit(0);
                           }
                                fwrite(&number,sizeof(int),1,fp);
                                fclose(fp);


                             if ((fp = fopen("employee.dat", "rb+")) == NULL)
                             {
                                   printf("Can't open file\n");
                                    exit(0);
                             }
                                 printf(" 添加成功!!\n");
    printf("      编号   姓名   性别   出生日期          职称          工作年限\n");
    for (i = 0; fread(&emp[i],sizeof(struct employee),1,fp) == 1 ; i++)
    {
        printf("       %s    %s    %s    %s          %s          %d\n",emp[i].num,emp[i].name,emp[i].sex,emp[i].shengri,emp[i].degree,emp[i].nianxian);
    }
    fclose(fp);
}
               
2012-09-03 21:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-380622-1-1.html




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

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