1. 程式人生 > >linux 程式碼實現資料夾及其檔案的

linux 程式碼實現資料夾及其檔案的

   #include<stdio.h>
   #include<stdlib.h>
   #include<string.h>
   #include<dirent.h>
   #include<sys/unistd.h>
   #include<sys/types.h>
   #include<fcntl.h>
   #include<sys/stat.h>
   
  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;
 }

執行示例: