C语言实现一个目录的拷贝

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
//拷贝文件
int copy_file(char *path1,char *path2)
{
    int fd1 =open(path1,O_RDONLY);                      //打开已存在的文件
    if(fd1<0){perror("fd1");return -1;}
    int fd2 = open(path2,O_RDWR|O_CREAT|O_TRUNC);       //新建新文件
    if(fd2<0){perror("fd2");return -1;}
    while(1)                              //拷贝,系统IO的拷贝,根据读取的字节数写入  
    {
        char buf[1024]={0};
        int size=read(fd1,buf,1024);
        if(size<=0)break;
        write(fd2,buf,size);
    }
    close(fd1);close(fd2);
    return 0;
}
//拷贝目录
void copy_dir(char *dir1, char *dir2)
{
    DIR *dp = opendir(dir1);
    mkdir(dir2, 0777);
    char path1[1024] = {0}, path2[1024] = {0};	//拼接路径
    readdir(dp);readdir(dp); //跳过 .. 与 .
    while (1)
    {
        struct dirent *msg &#
  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值