代码
1 #include <unistd.h>
2 #include <sys/stat.h>
3 #include <sys/types.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #define SIZE 8192
9
10 int main(int argc, char *argv[]){
11
12 char buf[SIZE];
13 int fd_src, fd_dest;
14 int len;
15
16 if (argc < 3){
17
18 printf("./mycp ssrc dest\n");
19 exit(1);
20 }
21
22 fd_src = open(argv[1],O_RDONLY);
23 fd_dest = open(argv[2],O_CREAT | O_WRONLY | O_TRUNC ,0666);
24
25 while((len = read(fd_src, buf, sizeof(buf))) > 0){
26
27 write(fd_dest, buf, len);
28 }
29
30 close(fd_src);
31 close(fd_dest);
32
33 }
~
~
执行
#gcc mycp.c -o mycp
#./mycp mycp.c mycp_cp.c
#cat mycp_cp.c