打印指定目录下所有文件
2013-05-18 11:50
274 查看
【Cocos2d-x】打印指定目录下所有文件
分类:
cocos2d-x2013-05-18 11:50270人阅读评论(0)收藏举报
Cocos2d-xcocos2d
无聊的时候写写代码,哇咔咔.以下是一个cocos2d-x打印指定目录下的所有文件.(跨平台的呦,C++初学者,代码污染请勿喷)...
[cpp]
view plaincopyprint?
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处添加如下代码:
[cpp]
view plaincopyprint?
#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】打印指定目录下所有文件
原文地址:http://acoder.me/print-the-specified-directory-of-all-documents.html
分类:
cocos2d-x2013-05-18 11:50270人阅读评论(0)收藏举报
Cocos2d-xcocos2d
无聊的时候写写代码,哇咔咔.以下是一个cocos2d-x打印指定目录下的所有文件.(跨平台的呦,C++初学者,代码污染请勿喷)...
[cpp]
view plaincopyprint?
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
}
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处添加如下代码:
[cpp]
view plaincopyprint?
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
#include <dirent.h>
#include <sys/stat.h>
#else
#include <io.h>
#endif
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) #include <dirent.h> #include <sys/stat.h> #else #include <io.h> #endif
Koyoter's Blog
原文标题:【Cocos2d-x】打印指定目录下所有文件
原文地址:http://acoder.me/print-the-specified-directory-of-all-documents.html
相关文章推荐
- Java——打印出指定目录中所有的文件 ,包括子文件夹中的所有文件
- PHP遍历并打印指定目录下所有文件实例
- 编写一个Python程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径
- Shell脚本递归打印指定目录中所有目录文件
- Linux目录扫描:打印指定目录下的所有文件及目录
- python 文件和目录 当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径。
- 采用多线程和生产者消费者模式来实现对于一个目录以及所有子目录下的文件内容的搜索,打印出包含指定关键字的行.
- PHP遍历并打印指定目录下所有文件实例
- 获取指定目录(包含子目录)下所有的txt文件的个数,并打印文件名称?
- 获取指定目录(包含子目录)下所有的txt文件的个数,并打印文件名称。程序1。
- 在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径
- 获取指定目录(包含子目录)下所有的txt文件的个数,并打印文件名称。程序2。
- 【代码】当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出绝对路径
- PHP遍历并打印指定目录下所有文件
- 遍历并打印指定目录下所有文件
- 编写一个程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径。
- linux下递归删除目录下所有指定的文件
- javaFile循环列出指定目录下的所有文件(源代码)
- Python使用reportlab将目录下所有的文本文件打印成pdf的方法
- findfirst findnext 递归查找指定目录下所有子目录下所有文件,为什么总是死机?