cocos2dx遍历文件夹和文件

string path =  CCFileUtils::sharedFileUtils()->fullPathForFilename("CloseNormal.png");
vector<string> str = GameTools::splitString(path,"/");
string strpath="";
for (int i=0;i<str.size()-1;i++)
{
strpath+=str[i]+"/";
}
CCLOG("****%s",path.c_str());
CCLOG("~~~~%s",strpath.c_str());

dfsFolder(strpath ,0);


void LodingScene::dfsFolder(string folderPath,int depth)
{
#ifdef WIN32
     _finddata_t FileInfo;
     string strfind = folderPath + "\\*";
     long Handle = _findfirst(strfind.c_str(), &FileInfo);
 
     if (Handle == -1L)
     {
         cerr << "can not match the folder path" << endl;
         exit(-1);
     }
     do{
         //判断是否有子目录
         if (FileInfo.attrib & _A_SUBDIR)    
         {
             //这个语句很重要
             if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0))   
             {
                 string newPath = folderPath + "\\" + FileInfo.name;
                 dfsFolder(newPath);
             }
         }
         else  
         {
             string filename = (folderPath + "\\" + FileInfo.name);
              //cout << folderPath << "\\" << FileInfo.name  << " " << endl;
CCLOG("%s",FileInfo.name);
         }
     }while (_findnext(Handle, &FileInfo) == 0);
 
     _findclose(Handle);
 #else
CCLOG("124");
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());
CCLOG("129--%s",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);
dfsFolder(entry->d_name,depth+4);
} else {
string filename = entry->d_name;
printf("%*s%s\n",depth,"",entry->d_name);
CCLOG("146---%s",filename.c_str());
}
}
    chdir("..");
    closedir(dp);
#endif
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值