-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy.c
More file actions
42 lines (36 loc) · 765 Bytes
/
copy.c
File metadata and controls
42 lines (36 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char* argv[])
{
int src_fd, dst_fd;
char buf[2048];
//判断输入
if (argc < 3)
{
printf("Usage: %s <src_file> <dst_file>\n", argv[0]);
return -1;
}
//打开文件原文件
if ((src_fd = open(argv[1], O_RDONLY | O_CREAT, 0666)) < 0)
{
perror("open");
return -1;
}
//打开目标文件
if ((dst_fd = open(argv[2], O_WRONLY | O_CREAT, 0666)) < 0)
{
perror("open");
return -1;
}
//复制
int n;
while ((n = read(src_fd, buf, 1024)) > 0)
{
write(dst_fd, buf, n);
}
//关闭文件描述符
close(src_fd);
close(dst_fd);
}