关于fread,fwrite问题求助,谢谢!
大家好,我最近初使用标准IO,遇到下面问题:我写了个电话号码的添加函数add_phone(),删除函数delete_phone()
问题:当我执行添加后,再执行删除,当全部都删除时,再添加,打印出来没问题;
但当我将添加和删除函数交替执行时(没有删除完时,再执行添加(add_phone())),就会出现问题了:
1.当删除函数(delete_phone)里read用:
while(!feof(fp1))
{
nread = fread(phone_delete1,sizeof(phone_delete1),1,fp1);
}
打印时可以打印出之前没有删除完的数据。而新添加的数据刚没有。
2.当删除函数(delete_phone)里read用:
while(1)
{
nread = fread(phone_delete1,sizeof(phone_delete1),1,fp1);
if(feof(fp1) == 0)
break;
}
打印时可以打印出之后添加(add_phone())的数据 ,而,之前没有删除完的,却不能打印。
但我是要全部都能显示出来啊。
这问题困扰我两天了,还不能解决。请各位大虾帮助看下,非常感谢。
我把代码附上来了,坐等大家帮忙! (为节省大家时间,中间段可不看,只帮忙看看那几个IO函数就行)
void add_phone(int *count1)//2.1添加电话号码
{
struct phonenumber phone_add[4];
struct phonenumber phone_add1[4];
int i,digit,protocol_choice,ret,j=4;
char cont;
int add_count = 0;
for(i=*count1+add_count;i<j;i++)
{
phone_add[i].order=i+1;
retype:
printf("请输入第%d个号码:",phone_add[i].order);
scanf("%s",phone_add[i].number);
if(strlen(phone_add[i].number) > 11)
{
printf("您输入号码长度超限\n");
goto retype;
}
else
{
for(digit=0;digit<12;digit++)
{
if(phone_add[i].number[digit] == 'c')
{
printf("请重新输入\n");
goto retype;
}
}
pro:
protocol();
scanf("%d",&protocol_choice);
fflush(stdin);
switch(protocol_choice)
{
case 1:
printf("**********\n");
printf("*正在保存*\n\n");
sleep(1);
printf("您输入的%d电话号码是:%s\n",phone_add[i].order,phone_add[i].number);
break;
case 2:
printf("**********\n");
printf("*正在保存*\n\n");
sleep(1);
printf("您输入的%d电话号码是:%s\n",phone_add[i].order,phone_add[i].number);
break;
case 3:
printf("**********\n");
printf("*正在保存*\n\n");
sleep(1);
printf("您输入的%d电话号码是:%s\n",phone_add[i].order,phone_add[i].number);
break;
case 4:
printf("**********\n");
printf("*正在保存*\n\n");
sleep(1);
printf("您输入的%d电话号码:%s\n",phone_add[i].order,phone_add[i].number);
break;
default:
goto pro;
break;
}//end switch
}//end else
input_con:
printf("是否继续(Y/N)\n");
scanf("%c",&cont);
cont = getchar();
if((cont == 'N') || (cont == 'n'))
{
(add_count)++;
break;
}
else if((cont == 'Y') ||(cont == 'y'))
{
(add_count)++;
printf("count1 = %d\n",add_count);
}
else
{
printf("input wrong\n");
goto input_con;
}
}//end for
for(i=*count1;i<*count1+add_count;i++)
{
FILE *phone_fp1 = fopen("./wenjian/phone.txt","a+");
if(!phone_fp1)
{
perror("faild open");
exit(-1);
}
printf("open sucess\n");
printf("order:%d nmuber:%s\n",phone_add[i].order,phone_add[i].number);
fwrite(phone_add,sizeof(phone_add),1,phone_fp1);
printf("write sucess\n");
fclose(phone_fp1);
}
for(i=*count1;i<*count1+add_count;i++)
{
printf("您输入的%d电话是:%s\n",phone_add[i].order,phone_add[i].number);
}
*count1+=add_count;
}
void delete_phone(int *count2)//2.2删除电话号码
{
struct phonenumber phone_delete1[4];
struct phonenumber phone_delete2[4];
int i,delete;
int j=0;
FILE *fp1,*fp2;
if ((fp1= fopen("./wenjian/phone.txt","r")) == NULL)
{
perror("open failed\n");
exit(-1);
}
printf("open sucess\n");
int nread;
while(!feof(fp1))
{
nread = fread(phone_delete1,sizeof(phone_delete1),1,fp1);
}
/*
while(1)
{
nread = fread(phone_delete1,sizeof(phone_delete1),1,fp1);
if (feof(fp1) == 0)
break;
}
*/
fclose(fp1);
printf("read sucess\n");
printf("coun2 = %d\n",*count2);
for(i=0;i<*count2;i++)
{
phone_delete1[i].order = i+1;
printf("您输入的%d电话号码是:%s\n",phone_delete1[i].order,phone_delete1[i].number);
}
input:
printf("请输入要删除编号:\n");
scanf("%d",&delete);
while((delete<=*count2))
{
strcpy(phone_delete1[delete-1].number,phone_delete1[delete].number);
delete++;
}
(*count2)--;
fp2 = fopen("./wenjian/phone.txt","w");
if (!fp2)
exit(-1);
for(i=0;i<*count2;i++)
{
fwrite(phone_delete1,sizeof(phone_delete1),1,fp2);
}
printf("delete write sucess\n");
fclose(fp2);
printf("剩下号码是:\n");
for(i=0;i<*count2;i++)
{
printf("编号:%d 号码:%s\n",phone_delete1[i].order,phone_delete1[i].number);
}
}