自己实现拷贝
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char** argv)
{
// linux 系统专用函数实现拷贝文件--------------------
int fd1, fd2, ret;
char buf[1024];
fd1 = open(argv[1], O_RDONLY);
fd2 = open(argv[2], O_WRONLY | O_CREAT, 0666);
while((ret = read(fd1, buf, sizeof(buf))) > 0)
{
write(fd2, buf, ret);
}
close(fd1);
close(fd2);
// 标准库 c 函数实现拷贝文件-----------------------
/*
FILE* fd1, *fd2;
int ret;
char buf[1024];
fd1 = fopen(argv[1], "rb");
fd2 = fopen(argv[2], "wb");
while((ret = fread(buf, 1, sizeof(buf), fd1)) > 0)
{
fwrite(buf, 1, ret, fd2);
}
fclose(fd1);
fclose(fd2);
*/
// 将文件内容打印到当前终端------------------------
/*
while(1)
{
fwrite("a", 1, 1, stdout);
fflush(stdout);
sleep(1);
}
*/
return 0;
}
在命令行输入参数示例:./a.out 123.c /root/456.c
./a.out :gcc编译后的可执行文件.
123.c :当前目录下的目标文件,如果要拷贝其他目录的文件,前面加上地址就行.
456.c :拷贝后的文件.