1. #if 0
  2. 需要注意的问题:
  3. 1:使用open函数的时候一定要注意其属性方面的选择,
  4. 需要写入的文件一定包含O_WRONLY,否则写的时候会报错由于没有写权限
  5. 读也是一样需要文件包含O_RDONLY
  6. 整个流程如下
  7. //打开文件
  8. //读前设置源文件当前位置
  9. //写前设置目标文件当前位置
  10. //读源文件
  11. //写目标文件
  12. //完成文件复制后关闭两个文件
  1. #endif
  2.  
  3. #include <sys/types.h> 
  4. #include <sys/stat.h> 
  5. #include <unistd.h> 
  6. #include <fcntl.h> 
  7. #include <stdlib.h> 
  8. #include <stdio.h> 
  9.  
  10.  
  11. #define buf_size 1024 
  12.  
  13.  
  14. unsigned char buf[buf_size]; 
  15.  
  16. int main(void) 
  17.         int src, dst; 
  18.         src = open("./src", O_CREAT|O_RDONLY, S_IRWXU|S_IRWXG|S_IROTH); 
  19.         dst = open("./dst", O_CREAT|O_TRUNC|O_RDONLY|O_WRONLY, S_IRWXU|S_IRWXG|S_IROTH); 
  20.         if(src<0 |dst<0
  21.         { 
  22.                 printf("fail to open files");            
  23.                 exit(1);     
  24.         }        
  25.          
  26.         lseek(src, 0, SEEK_SET); 
  27.          
  28.         ssize_t real_read; 
  29.         while((real_read = read(src,(void*)buf,(size_t)buf_size)) >0) 
  30.         {                                   
  31.                 if(write(dst, (const void*)buf, (size_t)real_read)<0
  32.                 { 
  33.                         printf("write\n"); 
  34.                         exit(1); 
  35.                 }                                  
  36.         } 
  37.          
  38.         if(real_read == -1) 
  39.         { 
  40.                 printf("read\n"); 
  41.                 exit(1); 
  42.         } 
  43.          
  44.         close(src); 
  45.         close(dst); 
  46.  
  47.         return 0;