求助!问题:文件输出最后一个重复输出和第一个不输出
这是一个结构数组写的简单地手机通讯录,它的问题是执行浏览操作的时候,会把最后一组数据重复输出。但是把浏览操作中 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);
}
}