#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#define BUFSIZE 16000000
int main(int argc, char *argv[]) {
int sfd, dfd;
char buf[BUFSIZE];
int len, ret, pos;
int count = 0;
if (argc < 3) {
fprintf(stderr, "Usage:...\n");
exit(1);
}
sfd = open(argv[1], O_RDONLY);
if (sfd < 0) {
perror("open()");
exit(1);
}
dfd = open(argv[2], O_WRONLY|O_CREAT, O_TRUNC, 0600);
if (dfd < 0) {
close(sfd);
perror("open()");
exit(1);
}
while (1) {
len = read(sfd, buf, BUFSIZE);
if (len < 0) {
perror("read()");
break;
}
if (len == 0) {
break;
}
ret = write(dfd, buf, len);
if (ret < 0) {
perror("write()");
break;
}
count++;
}
printf("count: %d\n", count);
//close file
close(dfd);
close(sfd);
exit(0);
}
通过系统调用IO实现文件拷贝
最新推荐文章于 2025-02-19 00:15:00 发布