文件夹及其文件的拷贝
首先先看看文件的拷贝:
在当前目录下创建一个文件 里面随便写点东西
vim copy.c:
#include
#include
#include
#include
#include
#include
#include
#define MAX 150
int main(int argc,char** argv)
{
int in;
int out;
int fd,fd1;
//源文件 test.txt
char *filename = "./test.txt";
char str[MAX];
fd=open(filename,O_RDWR);
if(fd==-1){
printf("打开失败n");
exit(1);
}
out =read(fd,str,MAX);
if(out==-1){
printf("读取失败n");
exit(1);
}
close(fd);
int size;
size=strlen(str);
printf("读出%d个字节n",size);
fd1=creat("./copy.txt",S_IRWXU|S_IRWXG); //目标文件copy.txt
if(fd1==-1){
printf("创建文件失败n");
exit(1);
}
in=write(fd1,str,size);
if(in==-1){
printf("写入失败n");
exit(1);
}
printf("写入成功n");
return 0;
}
#gcc -o copy copy.c
./copy
会发现该目录下多了个文件
转入正题 :实现文件和文件的拷贝
#include
#include
#include
#include
#include
#include
#include
#include
int is_dir(char* path) //判断是否是目录
{
struct stat st;
stat(path,&st);
if(S_ISDIR(st.st_mode)){
return 1;
}else{
return 0;}
}
/*字符串处理函数*/
int endwith(char* s,char c){
if(s[strlen(s)-1]==c){ //用于判断字符串结尾是否为"/"
return 1;
}else{
return 0;
}
}
char* str_contact(char* str1,char* str2){
char* result;
result=(char*)malloc(strlen(str1)+strlen(str2)+1);
if(!result){
printf("字符串连接时,内存动态分配失败n");
exit(1);
}
strcat(result,str1);
strcat(result,str2);
return result;
}
/* 复制函数 */
void copy_file(char* source_path,char* destination_path){
char buffer[1024];
int in,out,size;
int fd,fd1;
fd=open(source_path,O_RDWR);
if(fd==-1){
printf("打开失败n");
exit(1);
}
out=read(fd,buffer,1024);
if(out==-1){
printf("读取失败n");
exit(1);}
close(fd);
size=strlen(buffer);
fd1=open(destination_path,O_WRONLY|O_CREAT,S_IRWXU|S_IRWXG);
if(fd1==-1){
printf("打开失败n");
exit(1);
}
in=write(fd1,buffer,size);
if(in==-1){
printf("写入失败n");
exit(1);
}
}
//复制文件夹
void copy_folder(char* source_path,char* destination_path){
if(!opendir(destination_path)){
if(mkdir(destination_path,0777)) //如果不存在就用mkdir函数来创建
{
printf("创建文件失败!");
}
}
char* path;
path=(char*)malloc(512); //相当于其他语言的String path="",在纯C环境下字符串必须自己
管理大小
//这里path直接申请512的位置的空间,用于目录的拼接
path=str_contact(path,source_path);//这三句 相当于path=source_path
struct dirent* filename;
DIR* dp=opendir(path);
while(filename=readdir(dp)){
//遍历DIR指针指向的文件夹 也就是文件数组
memset(path,0,sizeof(path));
path=str_contact(path,source_path);
char* file_source_path;
file_source_path=(char*)malloc(512);
if(!endwith(source_path,'/')){
file_source_path=str_contact(file_source_path,source_path);
file_source_path=str_contact(source_path,"/");
}else{
file_source_path=str_contact(source_path,"/");
}
char *file_destination_path;
file_destination_path=(char*)malloc(512);
if(!endwith(destination_path,'/')){
file_destination_path=str_contact(file_destination_path,destination_path);
file_destination_path=str_contact(destination_path,"/");
}else{
file_destination_path=str_contact(file_destination_path,destination_path);
}
//取文件名与当前文件夹拼接成一个完整路径
file_source_path=str_contact(file_source_path,filename->d_name);
file_destination_path=str_contact(file_destination_path,filename->d_name);
if(is_dir(file_source_path)){
if(!endwith(file_source_path,'.')){
copy_folder(file_source_path,file_destination_path);
}
}else{
copy_file(file_source_path,file_destination_path);//否则按照单一文件的方法进
行复制
printf("复制%s到%s成功!n",file_source_path,file_destination_path);
}
}
}
int main(int argc,char* argv[]){
if(argv[1]==NULL||argv[2]==NULL){
printf("请输入两个文件夹路径,第一个为源,第二个为目的!n");
exit(1);}
char* source_path=argv[1];
char* destination_path=argv[2];
DIR* source=opendir(source_path);
DIR* destination=opendir(destination_path);
if(!source||!destination){
printf("你输入的第一个参数或者第二个参数不是文件夹!n");
}
copy_folder(source_path,destination_path);//进行文件的拷贝
return 0;
}
运行示例:
a里面的内容为 hello good body!
在/tmp 下有AAA/BBB/CCC/a (源路径)
还有 DDD(目标路径)
# gcc -o copy copy.c
./copy /tmp/AAA/BBB/CCC/a /tmp/DDD
运行后会发现 在/tmp/DDD下多了/BBB/CCC/a
完成了拷贝