求高手写一个C程序

来源:百度知道 编辑:UC知道 时间:2024/06/24 10:42:19
我有2个文件input0.txt含有数字:
1
2
3
4
5
6
input1.txt含有:
7
8
9
我需要分别对他们读取,相加输出总数和总行数,最后输出2个文件的总和
就是对每个文件算 行数 和总和,接着再求2者之和

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1,*fp2;
char st[10];
int sum=0,line=0;
if(NULL==(fp1=fopen("input0.txt","r")))
{printf("can't open the file");return 0;}
while(EOF!=fscanf(fp1,"%s\n",st))
sum+=atoi(st),line++;
fclose(fp1);
if(NULL==(fp2=fopen("input1.txt","r")))
{printf("can't open the file");return 0;}
while(EOF!=fscanf(fp2,"%s\n",st))
sum+=atoi(st),line++;
fclose(fp2);
printf("sum=%d,line=%d",sum,line);
return 0;
}
//将input0.txt和input1.txt和程序放在一个目录下
//运行得到"sum=45,line=9"

对应的行相加吗?没说清楚呀,你把结果写出来吧。