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
标签:arr,read,res,write,int,IO,path,include,open
From: https://blog.csdn.net/qq_51852604/article/details/139575165