您的位置:首页 > 编程语言 > C语言/C++

C实现复制文件

2016-06-09 10:32 211 查看

代码

//mycp.c

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 cp copy 复制