打印指定目录下所有文件

【Cocos2d-x】打印指定目录下所有文件       
        分类:            cocos2d-x 270人阅读 评论(0) 收藏 举报

无聊的时候写写代码,哇咔咔.以下是一个cocos2d-x打印指定目录下的所有文件.(跨平台的呦,C++初学者,代码污染请勿喷)...

  1. void HelloWorld::FindAllFile(string folderPath) 
  2.     // Window处理方式 
  3. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) 
  4.     _finddata_t FileInfo; 
  5.     string strfind = folderPath + "\\*"
  6.     long Handle = _findfirst(strfind.c_str(), &FileInfo); 
  7.     if (Handle == -1L) 
  8.     { 
  9.         CCLog("can not match the folder path"); 
  10.         return
  11.     } 
  12.     do
  13.         //判断是否有子目录 
  14.         if (FileInfo.attrib & _A_SUBDIR)     
  15.         { 
  16.             //这个语句很重要 
  17.             if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0))    
  18.             { 
  19.                 string newPath = folderPath + "\\" + FileInfo.name; 
  20.                 FindAllFile(newPath); 
  21.             } 
  22.         } 
  23.         else   
  24.         { 
  25.             count++; 
  26.             CCLog("%s\\%s" , folderPath.c_str() , FileInfo.name); 
  27.         } 
  28.     }while (_findnext(Handle, &FileInfo) == 0); 
  29.     _findclose(Handle); 
  30. #else 
  31.     CCLog(folderPath.c_str()); 
  32.     DIR *dp; 
  33.     struct dirent* dirp; 
  34.  
  35.     if((dp = opendir(folderPath.c_str())) == NULL) 
  36.     { 
  37.         CCLog("can not match the folder path"); 
  38.         return
  39.     } 
  40.     while ((dirp=readdir(dp))!=NULL) 
  41.     { 
  42.         struct stat buf; 
  43.         stat(folderPath.c_str(), &buf); 
  44.  
  45.         // 如果是目录 
  46.         if (S_ISDIR(buf.st_mode)) 
  47.         { 
  48.                 string path; 
  49.                 if( (strcmp(dirp->d_name,".") != 0 ) &&(strcmp(dirp->d_name,"..") != 0))    
  50.                 { 
  51.                     path =  folderPath + "/" + dirp->d_name; 
  52.                 } 
  53.                 //如果是目录,递归调用 
  54.                 FindAllFile(path);    
  55.         } 
  56.         else 
  57.         { 
  58.             // 如果是文件直接打印 
  59.             CCLog("%s/%s\n",folderPath.c_str(),dirp->d_name); 
  60.         } 
  61.     } 
  62.     CCLog("\n"); 
  63.     closedir(dp); 
  64. #endif 
void HelloWorld::FindAllFile(string folderPath)
{
	// Window处理方式
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
    _finddata_t FileInfo;
    string strfind = folderPath + "\\*";
    long Handle = _findfirst(strfind.c_str(), &FileInfo);
    if (Handle == -1L)
    {
		CCLog("can not match the folder path");
		return;
    }
    do{
        //判断是否有子目录
        if (FileInfo.attrib & _A_SUBDIR)    
        {
            //这个语句很重要
            if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0))   
            {
                string newPath = folderPath + "\\" + FileInfo.name;
                FindAllFile(newPath);
            }
        }
        else  
        {
			count++;
			CCLog("%s\\%s" , folderPath.c_str() , FileInfo.name);
        }
    }while (_findnext(Handle, &FileInfo) == 0);
    _findclose(Handle);
#else
	CCLog(folderPath.c_str());
	DIR *dp;
	struct dirent* dirp;

	if((dp = opendir(folderPath.c_str())) == NULL)
	{
		CCLog("can not match the folder path");
		return;
	}
	while ((dirp=readdir(dp))!=NULL)
	{
		struct stat buf;
		stat(folderPath.c_str(), &buf);

		// 如果是目录
		if (S_ISDIR(buf.st_mode))
        {
				string path;
				if( (strcmp(dirp->d_name,".") != 0 ) &&(strcmp(dirp->d_name,"..") != 0))   
				{
					path =  folderPath + "/" + dirp->d_name;
				}
				//如果是目录,递归调用
                FindAllFile(path);   
		}
		else
		{
			// 如果是文件直接打印
			CCLog("%s/%s\n",folderPath.c_str(),dirp->d_name);
		}
	}
	CCLog("\n");
	closedir(dp);
#endif
}

记得在#include处添加如下代码 :

  1. #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) 
  2.     #include <dirent.h> 
  3.     #include <sys/stat.h> 
  4. #else 
  5.     #include <io.h> 
  6. #endif 
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
	#include <dirent.h>
	#include <sys/stat.h>
#else
	#include <io.h>
#endif


更多Cocos2d-x开发博文尽在 Koyoter's Blog
原文标题:【Cocos2d-x】打印指定目录下所有文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值