cocos2d-x 实现目录的遍历 和 文件的全拷贝
2016-08-19 19:05
302 查看
#include <unistd.h> #include <stdio.h> #include <dirent.h> #include <sys/stat.h> bool mycopyfileios(string src,string dst) { FILE* fp1 =fopen(src.c_str(),"rb"); if (!fp1) { returnfalse; } FILE* fp2 =fopen(dst.c_str(),"wb"); if (!fp2) { fclose(fp1); returnfalse; } char txt[1000]; while (1) { int size =fread(txt, sizeof(char), 1000, fp1); if (size > 0) { int realsize =fwrite(txt, sizeof(char), size, fp2); if (size != realsize) { fclose(fp1); fclose(fp2); returnfalse; } } else { break; } } fclose(fp1); fclose(fp2); returntrue; } //注: string g_dstPath为目标目录 folderPath 为原目录 void blAndcpFolder( string folderPath, int depth) { DIR *dp; struct dirent *entry; struct stat statbuf; if((dp = opendir(folderPath.c_str())) ==NULL) { fprintf(stderr,"cannot open directory: %s\n",folderPath.c_str()); return; } chdir(folderPath.c_str()); while((entry = readdir(dp)) != NULL) { lstat(entry->d_name,&statbuf); if(S_ISDIR(statbuf.st_mode)) { if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0) continue; printf("%*s%s/\n",depth,"",entry->d_name); char buf1[1024] = ""; getcwd(buf1, 1024); //切换到目标目录下 if (chdir(g_dstPath.c_str()) != 0) { printf("+++切换目录失败:%s\n", g_dstPath.c_str()); } g_dstPath = g_dstPath + "/" + entry->d_name ;//更新目标目录 char buf2[1024] = ""; char* p1 = NULL; if ((p1 = strstr(g_dstPath.c_str(),"LOLI"))) { strcpy(buf2, p1); } //buf2 是相对路径 WorkAbsolutePath::CreateDirect(buf2);//创建目标目录 // 创建目录--> CCFileUtils::sharedFileUtils()->createFolder(dir); //printf("--创建目录:%s\n", buf2); //切回原目录下 if (chdir(buf1) != 0) { printf("+---+切换目录失败%s", buf1); } blAndcpFolder(entry->d_name,depth+4); } else { string filename = entry->d_name; printf("%*s%s\n",depth,"",entry->d_name); m_curFileNum++; //setProgress(m_curFileNum*100/m_totalFileNum); char buf3[1024] = ""; getcwd(buf3, 1024); char buf4[1024] = ""; sprintf(buf4, "%s/%s", buf3, entry->d_name); string src = buf4; string dst = g_dstPath + "/" + entry->d_name; mycopyfileios(src, dst); } } chdir(".."); closedir(dp); //回到上一级目录 char buf5[1024] = "", buf6[1024] = ""; getcwd(buf5, 1024); if (chdir(g_dstPath.c_str()) != 0) { printf("--------下面一部分切换失败--------\n"); } else{ chdir(".."); getcwd(buf6, 1024); g_dstPath = buf6; chdir(buf5); } }
相关文章推荐
- 目录下的文件拷贝,或者整个目录拷贝的实现
- [导入]Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(一)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- linux下递归遍历目录下的文件,实现批量替换
- PHP目录文件遍历实现
- Java 实现 文件目录 遍历 - Java 学习笔记 (22)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(一)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(一)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- Perl篇:递归遍历及拷贝文件共享服务器中目录
- 用C++实现遍历指定目录下的[指定文件后缀名]文件
- VB 中遍历目录,遍历目录查找文件的2个实现方法(挺好)
- 用递归算法实现ASP程序用fso对硬盘上某个路径下目录和文件的遍历
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(二)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(一)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- [导入]Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(二)
- shell 脚本实现的遍历目录修改子目录及其文件权限
- [转]Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)