又一个!
文本文件b.txt中包含若干整数,请把文件中的所有数据相加,并把累加和写入文件最后。#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
float sum=0;
int m;
if((fp=fopen("b.txt","a+"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%d",&m);
sum=sum+m;
}
fputc('\n',fp);
fprintf(fp,"%f",sum);
fclose(fp);
}