linux下文件复制

编写fcp.c文件

#include 
#include 
#include 
#include 
#include 

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查看与源文件内容相同证明内容复制成功!

你可能感兴趣的:(linux下文件复制)