cocos2d-x 实现目录的遍历 和 文件的全拷贝


#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);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值