文件IO——用read与write实现图片拷贝

  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <sys/stat.h>
  4 #include <fcntl.h>
  5 #include <unistd.h>
  6 #include <string.h>
  7 int main(int argc, const char *argv[])
  8 {
  9 
 10     int path= open("./1zh.jpg",O_RDONLY);
 11     if(path < 0)
 12     {
 13         perror("open");
 14         return -1;
 15     }
 16     printf("文件打开成功\n");
 17 
 18     int path1= open("./2.jpg",O_WRONLY | O_CREAT | O_TRUNC, 0777);
 19     if(path1 < 0)
 20     {
 21         perror("open");
 22         return -1;
 23     }
 24     printf("文件打开成功\n");
 25     char arr[32] = "";
 26     ssize_t res = 0;
 27     while(1)
 28     {
 29         bzero(arr,sizeof(arr));  
 30         res = read(path,arr,sizeof(arr));
 31         if(res <0)
 32         {
 33             perror("read");
 34             return -1;
 35         }
 36         else if (res == 0)
 37         {
 38             printf("文件读取完毕\n");
 39             break;
 40         }
 41         write(path1,arr,res);
 42     }
 43     if (close(path) <0)
 44     {
 45         perror("close");
 46         return -1;
 47     }
 48     printf("文件关闭成功\n");
 49 
 50     if (close(path1) <0)
 51     {
 52         perror("close");
 53         return -1;
 54     }
 55     printf("文件关闭成功\n");
 56     return 0;
 57 }
 58 

相关推荐

  1. 用户层read write io命令到NVMe SSD全流程

    2024-06-13 20:28:01       13 阅读
  2. binary.write 和 binary.read

    2024-06-13 20:28:01       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-13 20:28:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-13 20:28:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-13 20:28:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-13 20:28:01       20 阅读

热门阅读

  1. Web前端知道:深入探索与无尽挑战

    2024-06-13 20:28:01       7 阅读
  2. TCP协议

    TCP协议

    2024-06-13 20:28:01      10 阅读
  3. 科技发展对社会就业结构的影响与挑战

    2024-06-13 20:28:01       6 阅读
  4. C语言练习题04

    2024-06-13 20:28:01       5 阅读
  5. 【RAG入门教程04】Langchian的文档切分

    2024-06-13 20:28:01       7 阅读
  6. docker编译一个支持flv的nginx镜像

    2024-06-13 20:28:01       9 阅读
  7. Python基础(一)

    2024-06-13 20:28:01       7 阅读
  8. 【css】html 标初始化CSS样式(初学者必看)

    2024-06-13 20:28:01       11 阅读
  9. Jackson无缝替换Fastjson

    2024-06-13 20:28:01       5 阅读
  10. 在 Spring Boot 中实现文件上传功能

    2024-06-13 20:28:01       9 阅读
  11. TCP和UDP区别

    2024-06-13 20:28:01       4 阅读