C++ 实现文件/文件夹的复制、移动、删除等接口

0.头文件

#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <cstdio>

 

1、复制文件

 1 bool copyFile(const String& source, const String& target)
 2 {    
 3     bool bRet = false;    int iRet , iRead , size , iWrite;
 4     FILE* fS = NULL , *fD = NULL;
 5     char fileName[25] ={0};
 6     char buf[100] ="";
 7     char cmd[125] ="";
 8     char* buffer = NULL;
 9     int iPos = target.reverseFind('/');
10     
11     char* src = (char*)malloc(256*sizeof(char));
12     int src_len = strlen((char *)source.ascii().data());
13     memcpy(src, (char *)source.ascii().data(), src_len);
14     src[src_len] = '\0';
15    
16     char* des =  (char*)malloc(256*sizeof(char));
17     int des_len = strlen((char *)target.ascii().data());
18     memcpy(des, (char *)target.ascii().data(), des_len);
19     des[des_len] = '\0';
20 
21     if (iPos+1 != des_len)
22     {
23         strcat(des,"/");
24     }
25 
26     iRet = access(src, F_OK);
27     if (iRet) 
28     {
29       return false; //源文件不存在.
30     }
31     fS = fopen(src, "r");
32     if (fS == NULL) 
33     {
34       return false; //源文件打开失败
35     }
36 
37     DIR* dir;
38     if((dir = opendir(des)) == NULL)
39     {
40       goto finish;
41     } 
42     // strncpy(fileName,src+12,strlen(src) -12);//"/mnt/sdcard/"
43     strncpy(buf,des,strlen(des));
44     strcat(buf,fileName);
45 
46     sprintf(cmd, "touch %s",buf);
47     system(cmd);//创建目标文件
48     
49     fD = fopen(buf, "w+");
50     if (fD == NULL) 
51     {
52       goto finish;
53     }
54     fseek(fS, 0, SEEK_END);
55     size = ftell(fS);    //文件首尾偏移的字节数
56     fseek(fS, 0, SEEK_SET); //把fp指针移动到文件开头
57     buffer = (char*) calloc(1, size + 1); 
58     if (!buffer) 
59     {
60       goto finish;
61     }
62     iRead = fread(buffer, 1, size, fS);  //成功读取的元素个数
63     if (iRead != size) 
64     {
65       goto finish;
66     }
67     iWrite = fwrite((void*)buffer, 1,  size, fD); //写入目标文件
68     if (iWrite != size) 
69     {
70       goto finish;
71     }
72     bRet = true;
73     finish:
74     if (fS)
75         fclose(fS);
76     if (fD)
77         fclose(fD);
78     if (buffer)
79         free(buffer);
80     free(src);
81     src = NULL;
82     free(des);
83     des = NULL;
84     closedir(dir);
85     return bRet;
86 }

 

 

2、移动文件

 1 bool moveFile(const String& source, const String& target)
 2 {
 3     bool bRet = false;
 4     int iRet , iRead , size , iWrite ;
 5     FILE* fS = NULL , *fD = NULL;
 6     char fileName[25] ={0};
 7     char buf[100] ="";
 8     char cmd[125] ="";
 9     char* buffer = NULL;    
10     int iPos = target.reverseFind('/');
11 
12     char* src = (char*)malloc(256*sizeof(char));
13     int src_len = strlen((char *)source.ascii().data());
14     memcpy(src, (char *)source.ascii().data(), src_len);
15     src[src_len] = '\0';
16     
17     char* des =  (char*)malloc(256*sizeof(char));
18     int des_len = strlen((char *)target.ascii().data());
19     memcpy(des, (char *)target.ascii().data(), des_len);
20     des[des_len] = '\0';
21     if (iPos+1 != des_len)
22     {
23        strcat(des,"/");
24     }
25     
26     iRet = access(src, F_OK); 
27     if (iRet) 
28     {
29         return false;  //源文件不存在
30     }
31     fS = fopen(src, "r");
32     if (fS == NULL) 
33     {        
34         return false;  // 源文件打开失败
35     }
36 
37     DIR* dir;
38     if((dir = opendir(des)) == NULL)
39     {
40         goto finish; //目标路径不存在
41     } 
42 
43     // strncpy(fileName,src+12,strlen(src) -12);//"/mnt/sdcard/"
44     strncpy(buf,des,strlen(des));
45     strcat(buf,fileName);
46     LOGD("fileName= [%s] buf=%s",fileName,buf);
47     
48     sprintf(cmd, "touch %s",buf);
49     system(cmd);
50 
51 
52     fD = fopen(buf, "w+");
53     if (fD == NULL) 
54     {   
55         goto finish;
56     }
57 
58     fseek(fS, 0, SEEK_END);
59     size = ftell(fS);        
60     fseek(fS, 0, SEEK_SET); 
61     buffer = (char*) calloc(1, size + 1); 
62     
63     if (!buffer) 
64     {
65         goto finish;
66     }
67     iRead = fread(buffer, 1, size, fS); 
68     if (iRead != size) 
69     {
70         goto finish;
71     }
72     iWrite = fwrite((void*)buffer, 1,  size, fD);
73     if (iWrite != size) 
74     {
75         goto finish;
76     }
77     
78     if(remove(src)==0)  //删除源文件
79     {
80         bRet = true;
81     }
82 
83     finish:
84     if (fS)
85         fclose(fS);
86     if (fD)
87         fclose(fD);
88     if (buffer)
89         free(buffer);
90     free(src);
91     src = NULL;
92     free(des);
93     des = NULL;
94     closedir(dir);
95     return bRet;
96     
97 }        

 

3、删除文件

 1 bool deleteFile(const String& path)
 2 {
 3     bool bRet = false;  
 4     char* src = (char *)path.utf8().data();
 5     int iRet;
 6     iRet = access(src, F_OK);
 7     if (iRet)  //文件不存在
 8     {
 9     }
10     else
11     {                   
12         if(remove(src)==0) 
13     {
14             bRet = true;
15         }
16         else //移除文件失败
17         {
18         }
19     }
20     return bRet;
21 }        

 

4、判断文件是否存在

 1 bool existLocalFile(const String& path)
 2 {
 3     bool bRet = false;
 4     int iRet;
 5     char* src = (char *)path.utf8().data();
 6     iRet = access(src,F_OK);    
 7     if (iRet)  //源文件不存在
 8     {
 9     }
10     else
11     {
12          bRet = true;
13     }
14     return bRet;
15 }

 

5、复制文件夹

  1 bool copyDirectory(const String& source, const String& target)
  2 {
  3     bool bRet = false;
  4 
  5     char* src = (char*)malloc(256*sizeof(char));
  6     int src_len = strlen((char *)source.ascii().data());
  7     memcpy(src, (char *)source.ascii().data(), src_len);
  8     src[src_len] = '\0';
  9     
 10     char* dest =  (char*)malloc(256*sizeof(char));
 11     int des_len = strlen((char *)target.ascii().data());
 12     memcpy(dest, (char *)target.ascii().data(), des_len);
 13     dest[des_len] = '\0';
 14  
 15     DIR *dirp = NULL;
 16     int iRet;
 17     iRet = access(src, F_OK);
 18     if (iRet) 
 19     {  
 20         return false;  //源文件不存在
 21     }
 22     
 23     //1.open the dir
 24     if(NULL ==(dirp = opendir(src)))
 25     {
 26         return false;
 27     }
 28 
 29     struct dirent *entp = NULL;
 30     //2.read the dir
 31     while(NULL != (entp = readdir(dirp)))
 32     { 
 33         if( 0 == (strcmp(entp->d_name,"..")) || 0 == (strcmp(entp->d_name, ".")))
 34         {
 35             continue;
 36         }
 37 
 38         char src_buf[100] = "";
 39         char dest_buf[100] = "";
 40 
 41         sprintf(src_buf, "%s/%s", src, entp->d_name);
 42         sprintf(dest_buf, "%s/%s", dest, entp->d_name); 
 43 
 44         struct stat src_stat;
 45 
 46         if( stat(src_buf,&src_stat) )
 47         {   
 48             goto finish; //get stat failed
 49         }
 50         if( S_ISREG(src_stat.st_mode) )
 51         {     
 52             int iRet , iRead , size , iWrite;
 53             FILE* fS = NULL , *fD = NULL;
 54             char* buffer = NULL;
 55             iRet = access(src_buf, F_OK);
 56             if (iRet) 
 57             {
 58                 return false;
 59             }
 60             fS = fopen(src_buf, "r");
 61             if (fS == NULL) 
 62             {
 63                 return false;
 64             }
 65             fD = fopen(dest_buf, "w+");
 66             if (fD == NULL) 
 67             {
 68                 goto finish;
 69             }
 70 
 71             fseek(fS, 0, SEEK_END);
 72             size = ftell(fS);  
 73             fseek(fS, 0, SEEK_SET); 
 74             buffer = (char*) calloc(1, size + 1); 
 75             if (!buffer) 
 76             {
 77                 goto finish;
 78             }
 79             
 80             iRead = fread(buffer, 1, size, fS);
 81             if (iRead != size) 
 82             {
 83                 goto finish;
 84             }
 85             
 86             iWrite = fwrite((void*)buffer, 1,  size, fD);
 87             if (iWrite != size) 
 88             {
 89                 goto finish;
 90             }
 91             if (fS)
 92             fclose(fS);
 93             if (fD)
 94             fclose(fD);
 95             if (buffer)
 96             free(buffer);
 97         }
 98         else if( S_ISDIR(src_stat.st_mode) )
 99         { 
100             if( -1 == mkdir(dest_buf, src_stat.st_mode) )
101             {   
102                 goto finish;
103             }
104             copyDirectory(src_buf, dest_buf);  //if subdir, recursive call itself
105         }
106     }
107     bRet = true;
108 
109     finish:
110     if (dirp)
111         closedir(dirp);
112     free(src);
113     src = NULL;
114     free(dest);
115     dest = NULL;
116     return bRet;
117 }

 

6、移动文件夹

 1 bool moveDirectory(const String& source, const String& target)
 2 {
 3     bool bRet = false;
 4     
 5     char* dest =  (char*)malloc(256*sizeof(char));
 6     int des_len = strlen((char *)target.ascii().data());
 7     memcpy(dest, (char *)target.ascii().data(), des_len);
 8     dest[des_len] = '\0';
 9 
10     char* src = (char*)malloc(256*sizeof(char));
11     int src_len = strlen((char *)source.ascii().data());
12     memcpy(src, (char *)source.ascii().data(), src_len);
13     src[src_len] = '\0';
14    
15     int iRet;
16     iRet = access(src, F_OK);
17     if (iRet) 
18     {  
19         return false;
20     }
21     
22     if(copyDirectory(src,dest))
23     {
24         if (deleteDirectory(src))
25         {
26             bRet =true;
27         }
28         else
29         {
30         }
31     }
32     else
33     {
34     }
35     return bRet;
36 
37 }

 

7、删除文件夹

 1 bool deleteDirectory(const String& path)
 2 {
 3     char* src = (char*)malloc(256*sizeof(char));
 4     int src_len = strlen((char *)path.ascii().data());
 5     memcpy(src, (char *)path.ascii().data(), src_len);
 6     src[src_len] = '\0';
 7     
 8     DIR * dir;
 9     int iRet;
10     bool bRet = false;
11     iRet = access(src, F_OK);
12     if (iRet) 
13     {   
14         return false;
15     }
16     else
17     {
18         if((dir = opendir(src)) ==NULL)
19         {
20             return false;
21         }
22         struct dirent *entp = NULL;
23         while(NULL != (entp = readdir(dir)))
24         {   
25              if( 0 == (strcmp(entp->d_name,"..")) || 0 == (strcmp(entp->d_name, ".")))
26              {
27                 continue;
28              }
29             char src_buf[100] = "";
30             sprintf(src_buf, "%s/%s", src, entp->d_name);
31             LOGD("src_buf = %s",src_buf);
32             struct stat src_stat;
33             if( stat(src_buf,&src_stat) ) 
34             {
35                 goto finish;
36             }
37             
38             if( S_ISREG(src_stat.st_mode) )
39             {
40                 deleteFile(src_buf);
41             }
42             
43             else if( S_ISDIR(src_stat.st_mode) ) 
44             {
45                 deleteDirectory(src_buf);  
46             }
47                
48         }
49         if(remove(src)==0)
50         {
51             bRet = true;
52         } 
53         finish:
54         if (dir)
55             closedir(dir);
56         free(src);
57         src = NULL;
58         return bRet;
59     }
60 }

 

posted @ 2018-11-01 12:03  席纳霍霍  阅读(17617)  评论(0编辑  收藏  举报