手动创建两个文本文件text1.txt,text2.txt,要求编程创建text3.txt,实现text1.txt和text2.txt文件中除去首行和末尾对应的数据相加,三个文本的内容如下:
#include <stdio.h>
int main()
{
FILE *fp1 = fopen("text1.txt", "ab+");
if(fp1 == NULL)
{
perror("fopen1");
return -1;
}
FILE *fp2 = fopen("text2.txt", "ab+");
if(fp2 == NULL)
{
perror("fopen2");
return -1;
}
FILE *fp3 = fopen("text3.txt", "ab+");
if(fp3 == NULL)
{
perror("fopen3");
return -1;
}
int a = fgetc(fp1);
int b = fgetc(fp2);
while(a != EOF && b != EOF)
{
if(a < '0' || a > '9')
{
if(fputc(a,fp3) == EOF)
{
perror("fputc");
return -1;
}
}
if(a >= '0' && a <= '9')
{
int c = a + b - '0';
if(fputc(c,fp3) == EOF)
{
perror("fputc");
return -1;
}
}
a = fgetc(fp1);
b = fgetc(fp2);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}