zoukankan      html  css  js  c++  java
  • 操作系统——文件合并

    编程实现文件的合并

     1 include<stdio.h>
     2 #include<unistd.h>
     3 //
     4 #include<fcntl.h>
     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]='';
    14         }
    15         if(bytes<512)
    16         {
    17             ch[bytes]='';
    18         }
    19        printf("源文件1内容
    ");
    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]='';
    27                  close(fd2);
    28             }
    29                       
    30        }
    31 
    32               else
    33        {
    34               printf("ERROR:file open failure!
    目标写入文件未找到!!
    ");
    35        }
    36     
    37     close(fd2);
    38     close(fd1);
    39     }
    40     else
    41     {
    42         printf("ERROR:file open failure!
    源文件1未找到!
    ");
    43 
    44     }
    45 
    46           if((fd1=open("2.txt",O_RDONLY))!=-1)
    47         {
    48 /*
    49        printf("源文件2内容
    ");
    50        while((bytes1=read(fd1,ch1,512))==512)
    51         {
    52             ch1[bytes1]='';
    53         }
    54         if(bytes1<512)
    55         {
    56             ch1[bytes1]='';
    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!
    目标写入文件未找到!!
    ");
    72            }
    73            close(fd2);
    74        }
    75     else
    76     {
    77         printf("ERROR:file open failure!
    源文件2未找到!
    ");
    78 
    79     }
    80 
    81 close(fd1);
    82 }

    结果显示;

  • 相关阅读:
    VS Code 隐藏 .meta 文件
    CentOS7安装之后无法上网
    windows通过ssh方式访问CentOS7
    解决libc.so.6: version `GLIBC_2.18' not found问题
    Node.js ArrayBuffer 转为字符串
    centos7 tar, zip 解压文件命令(tar, zip)
    CentOS7安装 clang
    CentOS7开启 ssh 22端口
    MongoDB手册
    C++回调函数
  • 原文地址:https://www.cnblogs.com/WangYiqiang/p/9561861.html
Copyright © 2011-2022 走看看