作者在 2010-09-18 18:24:34 发布以下内容
#include<stdio.h>
int add(int x,int y,int z);
int total;
int sum=0;
int main(void)
{
FILE *fp;
int i,j,k,n,x,y,z;
fp=fopen("data.dat","r");
if(fp==NULL)
{
printf("open file failed!\n");
exit(1);
}
while (!feof(fp))
{
n=fscanf(fp,"%d %d %d",&x,&y,&z);
if(3==n) //这句很重要,因为feof()是根据字节来判断的,而'\0'为两个字节很可能多循环一次,所以要检查参数
{
sum=add(x,y,z);
total++;
}
}
printf("sum=%d\ntotal %d numbers\n",sum,total);
return 0;
}
int add(int x,int y,int z)
{
sum+=x+y+z;
return sum;
}
int add(int x,int y,int z);
int total;
int sum=0;
int main(void)
{
FILE *fp;
int i,j,k,n,x,y,z;
fp=fopen("data.dat","r");
if(fp==NULL)
{
printf("open file failed!\n");
exit(1);
}
while (!feof(fp))
{
n=fscanf(fp,"%d %d %d",&x,&y,&z);
if(3==n) //这句很重要,因为feof()是根据字节来判断的,而'\0'为两个字节很可能多循环一次,所以要检查参数
{
sum=add(x,y,z);
total++;
}
}
printf("sum=%d\ntotal %d numbers\n",sum,total);
return 0;
}
int add(int x,int y,int z)
{
sum+=x+y+z;
return sum;
}