主题:输入10个浮点数保存到二进制文件中,再追加若干数据,最后统计数据个数。以下C程序错在哪?谢谢解答。
#include<stdio.h>
void main()
{float a;
int i,s=0;
FILE *fp;
fp=fopen("d:\\file","wb");
if(fp==NULL)
{printf("File can't be created.\n");exit(1);}
else
for(i=1;i<=10;i++)
{scanf("%f",&a);
fprintf(fp,"%6.2f",a);}
fclose(fp);
fp=fopen("d:\\file","ab");
if(fp==NULL)
{printf("File can't be opened.\n");exit(1);}
else
{printf("Please input the added data:\n");
scanf("%f",&a);
while(a!=0)
{fprintf(fp,"%6.2f",a);
scanf("%f",&a);}
}
fclose(fp);
fp=fopen("d:\\file","rb");
if(fp==NULL)
{printf("File can't be opened.\n");exit(1);}
else
{fseek(fp,0L,2);
printf("%ld",ftell(fp));}
fclose(fp);
}
void main()
{float a;
int i,s=0;
FILE *fp;
fp=fopen("d:\\file","wb");
if(fp==NULL)
{printf("File can't be created.\n");exit(1);}
else
for(i=1;i<=10;i++)
{scanf("%f",&a);
fprintf(fp,"%6.2f",a);}
fclose(fp);
fp=fopen("d:\\file","ab");
if(fp==NULL)
{printf("File can't be opened.\n");exit(1);}
else
{printf("Please input the added data:\n");
scanf("%f",&a);
while(a!=0)
{fprintf(fp,"%6.2f",a);
scanf("%f",&a);}
}
fclose(fp);
fp=fopen("d:\\file","rb");
if(fp==NULL)
{printf("File can't be opened.\n");exit(1);}
else
{fseek(fp,0L,2);
printf("%ld",ftell(fp));}
fclose(fp);
}