编程实现文件的合并
1 include2 #include 3 // 4 #include 5 void main() 6 { 7 int fd,fd1,fd2,bytes,bytes1,bytes2; 8 char ch[513],ch1[513],ch2; 9 if((fd=open("1.txt",O_RDONLY))!=-1) 10 { 11 while((bytes=read(fd,ch,512))==512) 12 { 13 ch[bytes]='\0'; 14 } 15 if(bytes<512) 16 { 17 ch[bytes]='\0'; 18 } 19 printf("源文件1内容\n"); 20 printf("%s",ch); 21 close(fd); 22 if((fd2=open("3.txt",O_WRONLY))!=-1) 23 { 24 while((bytes2=write(fd2,ch,bytes))==bytes) 25 { 26 ch[bytes2]='\0'; 27 close(fd2); 28 } 29 30 } 31 32 else 33 { 34 printf("ERROR:file open failure!\n目标写入文件未找到!!\n"); 35 } 36 37 close(fd2); 38 close(fd1); 39 } 40 else 41 { 42 printf("ERROR:file open failure!\n源文件1未找到!\n"); 43 44 } 45 46 if((fd1=open("2.txt",O_RDONLY))!=-1) 47 { 48 /* 49 printf("源文件2内容\n"); 50 while((bytes1=read(fd1,ch1,512))==512) 51 { 52 ch1[bytes1]='\0'; 53 } 54 if(bytes1<512) 55 { 56 ch1[bytes1]='\0'; 57 } 58 printf("%s",ch1); 59 */ 60 61 if((fd2=open("3.txt",O_APPEND|O_WRONLY))!=-1) 62 { 63 while((read(fd1,&ch2,1))==1) 64 { 65 write(fd2,&ch2,1); 66 } 67 68 } 69 else 70 { 71 printf("ERROR:file open failure!\n目标写入文件未找到!!\n"); 72 } 73 close(fd2); 74 } 75 else 76 { 77 printf("ERROR:file open failure!\n源文件2未找到!\n"); 78 79 } 80 81 close(fd1); 82 }
结果显示;