| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 533 人关注过本帖
标题:求助!问题:文件输出最后一个重复输出和第一个不输出
取消只看楼主 加入收藏
影随风
Rank: 2
等 级:论坛游民
帖 子:38
专家分:24
注 册:2013-3-14
结帖率:85.71%
收藏
已结贴  问题点数:20 回复次数:0 
求助!问题:文件输出最后一个重复输出和第一个不输出
这是一个结构数组写的简单地手机通讯录,它的问题是执行浏览操作的时候,会把最后一组数据重复输出。
但是把浏览操作中 while(!feof(fp))
    {
        fscanf(fp,"%s%s%s%s%s%s%s%s",g.name,g.age,g.tele,g.qq,g.weixin,g.bd,g.email,g.sex);
        printf("%s%4s%15s%11s%11s%9s%18s%4s\n",g.name,g.age,g.tele,g.qq,g.weixin,g.bd,g.email,g.sex);
    }
的 while(!feof(fp))换成while( fscanf(fp,"%s%s%s%s%s%s%s%s",g.name,g.age,g.tele,g.qq,g.weixin,g.bd,g.email,g.sex)!=E0F)时会丢掉第一组数据,求帮助!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct friends_list
{
 char  name[20],weixin[20],email[30],sex[4],age[10],qq[20],bd[10],tele[14];   
};
int Count=0;
void new_friend(struct friends_list frients[]);
void liulan_friend(struct friends_list friends[]);

int main(void)
{
    int choice;

 struct friends_list friends[1000];
 do
 {
  printf("1:新建 2:浏览 0:退出\n ");
  printf("请选择0~2号功能键\n");
  scanf("%d",&choice);
        switch(choice)
        {
        case 1:
            new_friend(friends);   
            break;
        case 2:
            liulan_friend(friends);
            break;
        case 0: break;
        }
}while(choice!=0);
   printf("感谢您使用本通讯录,欢迎下次使用\n");
   
  return 0;
}
 /*新建通讯录*/
void new_friend(struct friends_list friends[])
{
    struct friends_list f;
    FILE  *fp;
   
    if(Count==1000)
    {
     printf("通讯录已满\n");                        
                                                                                                
    return ;
    }
    else
    {
        printf("请输入新的联系人\n姓名:");
        scanf("%s",f.name);
        printf("年龄:\n");
        scanf("%s",f.age);
        printf("电话:\n");
        scanf("%s",f.tele);
        printf("qq:\n");
        scanf("%s",f.qq);
        printf("微信:\n");
        scanf("%s",f.weixin);
        printf("生日:\n");
        scanf("%s",f.bd);
        printf("电子邮件:\n");
        scanf("%s",f.email);
        printf("性别:\n");
        scanf("%s",f.sex);
        
        if((fp=fopen("f1.txt","a+"))==NULL)
        {
            printf("File open error!\n");
            exit(0);
        }
        fprintf(fp,"%s%4s%15s%11s%11s%9s%18s%4s\n",f.name,f.age ,f.tele,f.qq,f.weixin,f.bd,f.email,f.sex);

        if(fclose(fp))
        {
            printf("Can not close the file !\n");
            exit(0);
        }
    }
     Count++;
}
/*浏览操作*/
void liulan_friend(struct friends_list friends[])
{
    int i=0;        
    struct  friends_list g;
    FILE  *fp=fopen("f1.txt","r");
    printf(" 姓名  年龄  电话          QQ        微信      生日         电子邮件     性别  \n");
    if(!fp)
    {
        printf("File open error!\n");   
    }
    while(!feof(fp))
    {
        fscanf(fp,"%s%s%s%s%s%s%s%s",g.name,g.age,g.tele,g.qq,g.weixin,g.bd,g.email,g.sex);
        printf("%s%4s%15s%11s%11s%9s%18s%4s\n",g.name,g.age,g.tele,g.qq,g.weixin,g.bd,g.email,g.sex);
    }
    if(fclose(fp))
    {
        printf("Can not close the file!\n");
        exit(0);
    }
   
}
2013-05-14 13:12
快速回复:求助!问题:文件输出最后一个重复输出和第一个不输出
数据加载中...
 
   



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

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