本文介绍了win32、androID下文件夹的创建以及递归删除,下述代码都可直接在Cocos中调用头文件:#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)#include <sys/types.h>#include <sys/stat.h>#include <errno.h> #include <stdio.h>#include <dirent.h>#endif代码://AndroID or win32 创建文件夹bool DLManager::createDirectory(const char * path){#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) mode_t processMask = umask(0); int ret = mkdir(path,S_IRWXU | S_IRWXG | S_IRWXO); umask(processMask); if (ret != 0 && (errno != EEXIST)) { cclog("mkdir Failed..."); return false; } return true;#else BOol ret = CreateDirectoryA(path,NulL); if (!ret && ERROR_ALREADY_EXISTS != GetLastError()) { return false; } return true;#endif}//AndroID or win32 递归删除文件夹bool DLManager::deleteDirectory(const char * path){#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) DeleteDirectory(path);#else return DeleteDirectoryWin32(path); #endif}//AndroID 下判断是否为文件夹————引用自http://blog.csdn.net/honty/article/details/7822923int DLManager::IsDirectory(const char * filename) { #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) struct stat buf; int ret = stat(filename,&buf); if(0 == ret) { if(buf.st_mode & S_IFDIR) { //printf("%s is folder\n",filename); return 0; } else { //printf("%s is file\n",filename); return 1; } } return -1; #endif return -1;} //AndroID 下递归删除文件夹————引用自http://blog.csdn.net/honty/article/details/7822923int DLManager::DeleteDirectory(const char * dirname) { #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) char chBuf[256]; DIR * dir = NulL; struct dirent *ptr; int ret = 0; dir = opendir(dirname); if(NulL == dir) { return -1; } while((ptr = readdir(dir)) != NulL) { ret = strcmp(ptr->d_name,"."); if(0 == ret) { continue; } ret = strcmp(ptr->d_name,".."); if(0 == ret) { continue; } snprintf(chBuf,256,"%s/%s",dirname,ptr->d_name); ret = IsDirectory(chBuf); if(0 == ret) { //printf("%s is dir\n",chBuf); ret = DeleteDirectory(chBuf); if(0 != ret) { return -1; } } else if(1 == ret) { //printf("%s is file\n",chBuf); ret = remove(chBuf); if(0 != ret) { return -1; } } } (voID)closedir(dir); ret = remove(dirname); if(0 != ret) { return -1; } return 0; #endif return -1;} //win32 下判断是否为文件夹————引用自http://blog.csdn.net/jaff20071234/article/details/6559533bool DLManager::IsDirectoryWin32(const char *pDir) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) char szCurPath[500]; ZeroMemory(szCurPath,500); sprintf_s(szCurPath,500,"%s//*",pDir); WIN32_FIND_DATAA FindfileData; ZeroMemory(&FindfileData,sizeof(WIN32_FIND_DATAA)); HANDLE hfile = FindFirstfileA(szCurPath,&FindfileData); /**< find first file by given path. */ if( hfile == INVALID_HANDLE_VALUE ) { FindClose(hfile); return FALSE; /** 如果不能找到第一个文件,那么没有目录 */ }else { FindClose(hfile); return TRUE; } #endif return false;} //win32 下递归删除文件夹————引用自http://blog.csdn.net/jaff20071234/article/details/6559533 bool DLManager::DeleteDirectoryWin32(const char * Dirname) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)//http://blog.csdn.net/jaff20071234/article/details/6559533// CfileFind tempFind; //声明一个CfileFind类变量,以用来搜索 char szCurPath[MAX_PATH]; //用于定义搜索格式 _snprintf(szCurPath,MAX_PATH,"%s//*.*",Dirname); //匹配格式为*.*,即该目录下的所有文件 WIN32_FIND_DATAA FindfileData; ZeroMemory(&FindfileData,sizeof(WIN32_FIND_DATAA)); HANDLE hfile = FindFirstfileA(szCurPath,&FindfileData); BOol IsFinded = TRUE; while(IsFinded) { IsFinded = FindNextfileA(hfile,&FindfileData); //递归搜索其他的文件 if( strcmp(FindfileData.cfilename,".") && strcmp(FindfileData.cfilename,"..") ) //如果不是"." ".."目录 { string strfilename = ""; strfilename = strfilename + Dirname + "//" + FindfileData.cfilename; string strTemp; strTemp = strfilename; if( IsDirectoryWin32(strfilename.c_str()) ) //如果是目录,则递归地调用 { printf("目录为:%s/n",strfilename.c_str()); DeleteDirectoryWin32(strTemp.c_str()); } else { DeletefileA(strTemp.c_str()); } } } FindClose(hfile); BOol bRet = RemoveDirectoryA(Dirname); if( bRet == 0 ) //删除目录 { printf("删除%s目录失败!/n",Dirname); return FALSE; } return TRUE; #endif return false;}总结
以上是内存溢出为你收集整理的Cocos2.2.3文件夹的创建与递归删除全部内容,希望文章能够帮你解决Cocos2.2.3文件夹的创建与递归删除所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)