linux下文件复制

来源:互联网 发布:java多线程编程实例 编辑:程序博客网 时间:2024/06/05 07:02

编写fcp.c文件

#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>void main(int argc, char **argv){    int fd_s;    int fd_t;    int count = 0;    char buf[512];/*1.打开源文件*/    fd_s = open(argv[1],O_RDONLY);/*2.打开目标文件*/    fd_t = open(argv[2],O_RDWR|O_CREAT,0666);/*3读取源文件数据*/    while ((count = read(fd_s,buf,512)) >0)    {        /*4.写入目标文件*/        write(fd_t,buf,count);    }/*5关闭文件*/    close(fd_s);    close(fd_t);}

编译:
gcc fcp.c -o fcp

当前目录下创建源文件:s.txt

vim s.txt
写入任意数据保存

运行程序:
./fcp ./s.txt ./t.txt

最后打开t.txt查看与源文件内容相同证明内容复制成功!

1 0