您的位置:首页 > 移动开发 > Cocos引擎

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cocos2d-x c++
相关文章推荐