实现文件的读写操作举例

来源:互联网 发布:mac finder 打开路径 编辑:程序博客网 时间:2024/04/28 12:44

手动创建两个文本文件text1.txt,text2.txt,要求编程创建text3.txt,实现text1.txt和text2.txt文件中出去begin,end 对应数据相加,三个文本内容如下:

text1.txt:

begin 10 11 12 20 21 22 30 31 32 end

text2.txt:

begin 15 16 17 25 26 27 35 36 37 end

text3.txt:

begin 2 27 29 45 47 49 65 67 69 end

代码如下:

#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>int main(){FILE * fp1;FILE * fp2;FILE * fp3;int c1,c2,c3;    fp1 = fopen("text1.txt","r");    fp2 = fopen("text2.txt","r");fp3 = fopen("text3.txt","w+");while(((c1 = fgetc(fp1)) != EOF) && ((c2 = fgetc(fp2)) != EOF)){if(((c1 >= 'a') && (c2 <= 'z')) || ((c1 == ' ') &&(c2 == ' '))){c3 = c1;            fputc(c3,fp3);}else{            c3 = (c1 - '0') + (c2 - '0') + '0';fputc(c3,fp3);}}close(fp1);close(fp2);close(fp3);}

0 0