//代码说明: 只能源目录里面的文件,以文件名以 ‘.’ 开头无法复制
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//复制单个文件
void copydir(const char *d_name, const char *argv2);
int main(int argc, char **argv)
{
if (argc != 3)
{
printf("请输入两个目录路径\n");
exit(1);
}
//创建目录,权限全开
mkdir(argv[2], S_IRWXU | S_IRWXG | S_TROTH | S_IXOTH);
DIR *dp = opendir(argv[1]); //打开目录但没有进入目录
int i = 0; //计数君
struct dirent *ep = NULL;
while (1)
{
if(chdir(argv[1])) //进入源目录
{
perror("进入源文件夹失败");
exit(1);
}
ep = readdir(dp); //目录指针
if (ep != NULL)
{
if(ep->d_name == '.')
continue;
else
{
copydir(ep->d_name, argv[2]);
i++;
}
}
else if((ep == NULL) && (errno == 0))
break;
else
{
perror("复制数据出错");
}
}
}
void copydir(const char *d_name, const char *argv2)
{
FILE *fp1 = fopen(d_name, "r");
if(chdir(argv2)) //进入目标目录
{
perror("进入目标文件夹出错");
exit(1);
}
FILE *fp2 = fopen(d_name, "w");
char *buf = calloc(100, 1); //文件复制缓冲区
while (1) //复制动作
{
long a = ftell(fp1);
if(!(fread(buf, 100, 1, fp1)))
{
long b = ftell(fp1);
fwrite(buf, b-a, 1, fp2);
break;
}
fwrite(buf, 100, 1, fp2);
}
free(buf);
fclose(fp1);
fclose(fp2);
}
Linux下目录复制
猜你喜欢
转载自blog.csdn.net/qq_41985711/article/details/81474517
今日推荐
周排行