#include<stdio.h> #include<stdlib.h> #include <string.h> int main() { FILE *fp; FILE *pf; FILE *af; char ch1; char ch2; char ch3; int sum; if((fp=fopen("text1.text","rt")) == NULL) { printf("\nCannot open file1 strike any key exit!"); getchar(); exit(1); } if((pf = fopen("text2.text","rt")) == NULL) { printf("Cannot open file2,strike any key exit!"); getchar(); exit(1); } if((af = fopen("text3.text","wt")) == NULL) { printf("Cannot open file3,strike any key exit!"); getchar(); exit(1); } while (1) { ch1 = fgetc(fp); if (ch1 == EOF) { break; } ch2 = fgetc(pf); if (ch2 == EOF) { break; } if (ch1 >= '0' && ch1 <= '9') { sum = ch1 - '0' + ch2 - '0'; ch3 = sum + '0'; fputc(ch3,af); } else { fputc(ch2,af); } } printf("\n"); fclose(fp); fclose(pf); fclose(af); }
c语言文件操作:将文件text1和文件text2中的相加并存入文件text3中
最新推荐文章于 2023-02-08 22:44:57 发布