linuc 文件夹的拷贝


转自:http://blog.csdn.net/yongh701/article/details/50403256


在《【Linux】利用C语言文件流复制单一文件》(点击打开链接)讲述了如何用C语言拷贝文件,但是这只能拷贝单一文件。如果你要用LinuxC拷贝整个文件夹,同样要像《【Java】利用文件输入输出流完成把一个文件夹内的所有文件拷贝的另一的文件夹的操作》(点击打开链接)一样,先用《【Linux】遍历某一目录,判断文件与文件夹,main参数》(点击打开链接)的方法遍历整个文件目录,之后再一个一个实现文件拷贝。具体代码如下:

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<string.h>  
  4. #include<dirent.h>//输出文件信息  
  5. #include<sys/stat.h>//判断是否目录  
  6. int is_dir(char* path){//判断是否是目录  
  7.     struct stat st;  
  8.     stat(path,&st);  
  9.     if(S_ISDIR(st.st_mode)){  
  10.         return 1;  
  11.     }  
  12.     else{  
  13.         return 0;  
  14.     }  
  15. }  
  16. /*字符串处理函数*/  
  17. int endwith(char* s,char c){//用于判断字符串结尾是否为“/”  
  18.     if(s[strlen(s)-1]==c){  
  19.         return 1;  
  20.     }  
  21.     else{  
  22.         return 0;  
  23.     }  
  24. }  
  25. char* str_contact(char* str1,char* str2){//字符串连接  
  26.     char* result;  
  27.     result=(char*)malloc(strlen(str1)+strlen(str2)+1);//str1的长度+str2的长度+\0;  
  28.     if(!result){//如果内存动态分配失败  
  29.         printf("字符串连接时,内存动态分配失败\n");  
  30.         exit(1);  
  31.     }  
  32.     strcat(result,str1);  
  33.     strcat(result,str2);//字符串拼接  
  34.     return result;  
  35. }  
  36. /*复制函数*/  
  37. void copy_file(char* source_path,char *destination_path){//复制文件  
  38.     char buffer[1024];  
  39.     FILE *in,*out;//定义两个文件流,分别用于文件的读取和写入int len;  
  40.     if((in=fopen(source_path,"r"))==NULL){//打开源文件的文件流  
  41.         printf("源文件打开失败!\n");  
  42.         exit(1);  
  43.     }  
  44.     if((out=fopen(destination_path,"w"))==NULL){//打开目标文件的文件流  
  45.         printf("目标文件创建失败!\n");  
  46.         exit(1);  
  47.     }  
  48.     int len;//len为fread读到的字节长  
  49.     while((len=fread(buffer,1,1024,in))>0){//从源文件中读取数据并放到缓冲区中,第二个参数1也可以写成sizeof(char)  
  50.         fwrite(buffer,1,len,out);//将缓冲区的数据写到目标文件中  
  51.     }  
  52.     fclose(out);  
  53.     fclose(in);  
  54. }  
  55. void copy_folder(char* source_path,char *destination_path){//复制文件夹  
  56.     if(!opendir(destination_path)){  
  57.         if (mkdir(destination_path,0777))//如果不存在就用mkdir函数来创建  
  58.         {  
  59.             printf("创建文件夹失败!");  
  60.         }  
  61.     }  
  62.     char *path;  
  63.     path=(char*)malloc(512);//相当于其它语言的String path="",纯C环境下的字符串必须自己管理大小,这里为path直接申请512的位置的空间,用于目录的拼接  
  64.     path=str_contact(path,source_path);//这三句,相当于path=source_path  
  65.     struct dirent* filename;  
  66.     DIR* dp=opendir(path);//用DIR指针指向这个文件夹  
  67.     while(filename=readdir(dp)){//遍历DIR指针指向的文件夹,也就是文件数组。  
  68.         memset(path,0,sizeof(path));  
  69.         path=str_contact(path,source_path);  
  70.         //如果source_path,destination_path以路径分隔符结尾,那么source_path/,destination_path/直接作路径即可   
  71.         //否则要在source_path,destination_path后面补个路径分隔符再加文件名,谁知道你传递过来的参数是f:/a还是f:/a/啊?  
  72.         char *file_source_path;  
  73.         file_source_path=(char*)malloc(512);  
  74.         if(!endwith(source_path,'/')){  
  75.             file_source_path=str_contact(file_source_path,source_path);  
  76.             file_source_path=str_contact(source_path,"/");  
  77.         }  
  78.         else{  
  79.             file_source_path=str_contact(file_source_path,source_path);  
  80.         }  
  81.         char *file_destination_path;  
  82.         file_destination_path=(char*)malloc(512);  
  83.         if(!endwith(destination_path,'/')){  
  84.             file_destination_path=str_contact(file_destination_path,destination_path);  
  85.             file_destination_path=str_contact(destination_path,"/");  
  86.         }  
  87.         else{  
  88.             file_destination_path=str_contact(file_destination_path,destination_path);  
  89.         }  
  90.         //取文件名与当前文件夹拼接成一个完整的路径  
  91.         file_source_path=str_contact(file_source_path,filename->d_name);  
  92.         file_destination_path=str_contact(file_destination_path,filename->d_name);  
  93.         if(is_dir(file_source_path)){//如果是目录  
  94.             if(!endwith(file_source_path,'.')){//同时并不以.结尾,因为Linux在所有文件夹都有一个.文件夹用于连接上一级目录,必须剔除,否则进行递归的话,后果无法相像  
  95.                 copy_folder(file_source_path,file_destination_path);//进行递归调用,相当于进入这个文件夹进行复制~  
  96.             }         
  97.         }  
  98.         else{  
  99.             copy_file(file_source_path,file_destination_path);//否则按照单一文件的复制方法进行复制。  
  100.             printf("复制%s到%s成功!\n",file_source_path,file_destination_path);  
  101.         }  
  102.     }     
  103. }  
  104. /*主函数*/  
  105. int main(int argc,char *argv[]){  
  106.     if(argv[1]==NULL||argv[1]==NULL){  
  107.         printf("请输入两个文件夹路径,第一个为源,第二个为目的!\n");  
  108.         exit(1);  
  109.     }  
  110.     char* source_path=argv[1];//取用户输入的第一个参数  
  111.     char* destination_path=argv[2];//取用户输入的第二个参数  
  112.     DIR* source=opendir(source_path);  
  113.     DIR* destination=opendir(destination_path);  
  114.     if(!source||!destination){  
  115.         printf("你输入的一个参数或者第二个参数不是文件夹!\n");  
  116.     }  
  117.     copy_folder(source_path,destination_path);//进行文件夹的拷贝  
  118.     return 0;  
  119. }  

运行结果如下,设置一个有文件夹,有文件的文件夹A,计划将里面的内容,拷贝到原本空空是也的文件夹B中。


上述程序中,难点有以下几个:

1、由于此程序涉及多次文件路径的拼接,因此最大的难题就是C语言中对于字符串的处理。众所周知,C语言中是没有字符串的概念,仅有字符数组,与指向这个字符数组的首位置的指针的概念。用char *path;配合path=(char*)malloc(512);相当于搞出一个,指向长达512的空字符数组的指针,此时path你可以理解为字符串。这样使用的strcat函数,不停地给path进行拼接,就不会出现段错误的内存溢出错误。

接下去用了大量的代码用于源与目标文件路径与文件名的拼接,其实完成的功能很简单,如果是其它语言,可能string file_source_path;string filename->d_name;之后就一句file_source_path+filename->d_name;

反正大家明白什么回事就行了,不必细究,我也不过是把《【Linux】纯C环境下字符串的处理》(点击打开链接)中方法搞过来。

2、还有一个问题,就是复制的时候,要注意排除当前文件夹中.与..这两个链接到上级目录与根目录的文件夹。在Linux似乎任何一个文件夹都有这个东东,一开始我写程序的时候,根本不知道,搞了一个复杂度超高还根本停不下来的递归迭代,这也是Linux与Windows不一样的地方。

3、其它地方也就没什么了,都是一些之前文件写过的东西。

猜你喜欢

转载自blog.csdn.net/fly_sky_share/article/details/78572707