Linux 文件I/O操作(简单实现文件复制)

 

       简单的实现一下文件的复制操作,直接贴源码了,中间也有一些注释,至于更多的详细的命令参数,推荐看下这篇博客,讲的很详细:传送门

 

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define maxn 1005

int main(int agrc, char *agrv[])
{
        if(agrc < 3){   // 如果传入的参数不够3个直接退出
                printf("./copy error!\n");
                exit(0);
        }
        int len;
        char buf[maxn];
        int fd_file = open(agrv[1], O_RDONLY);     // open一个只能读的文件 在agrv[1]中
        // open一个只能写的文件 如果不存在就新创建一个 如果存在O_TRUNC可以将其内容大小设置为0
        // 因为有O_CREAT参数 所以最后还需要设置文件权限
        int fd_aim = open(agrv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
        // while循环不断从fd_file中读取数据
        while((len = read(fd_file, buf, sizeof(buf))) > 0){
                write(fd_aim, buf, len);     // 将读到的数据写入fd_aim,注意长度为len
        }
        return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值