日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

cocos2d-x 实现跨平台的目录遍历

發(fā)布時(shí)間:2023/12/4 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cocos2d-x 实现跨平台的目录遍历 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

可能各位看官們有更好的方法,請不吝賜教。

#ifdef _WIN32 #include <io.h> #else #include <unistd.h> #include <stdio.h> #include <dirent.h> #include <sys/stat.h> #endif

win32平臺(tái)引用io.h里面的信息。

主要定義了一個(gè)void dfsFolder(string folderPath, int depth = 0);方法對(duì)其進(jìn)行遞歸遍歷。

具體的實(shí)現(xiàn)為

1 void dfsFolder( string folderPath, int depth) 2 { 3 #ifdef WIN32 4 _finddata_t FileInfo; 5 string strfind = folderPath + "\\*"; 6 long Handle = _findfirst(strfind.c_str(), &FileInfo); 7 8 if (Handle == -1L) 9 { 10 cerr << "can not match the folder path" << endl; 11 exit(-1); 12 } 13 do{ 14 //判斷是否有子目錄 15 if (FileInfo.attrib & _A_SUBDIR) 16 { 17 //這個(gè)語句很重要 18 if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0)) 19 { 20 string newPath = folderPath + "\\" + FileInfo.name; 21 dfsFolder(newPath); 22 } 23 } 24 else 25 { 26 string filename = (folderPath + "\\" + FileInfo.name); 27 28 cout << folderPath << "\\" << FileInfo.name << " " << endl; 29 } 30 }while (_findnext(Handle, &FileInfo) == 0); 31 32 _findclose(Handle); 33 #else 34 DIR *dp; 35 struct dirent *entry; 36 struct stat statbuf; 37 if((dp = opendir(folderPath.c_str())) == NULL) { 38 fprintf(stderr,"cannot open directory: %s\n", folderPath.c_str()); 39 return; 40 } 41 chdir(folderPath.c_str()); 42 while((entry = readdir(dp)) != NULL) { 43 lstat(entry->d_name,&statbuf); 44 if(S_ISDIR(statbuf.st_mode)) { 45 46 if(strcmp(".",entry->d_name) == 0 || 47 strcmp("..",entry->d_name) == 0) 48 continue; 49 printf("%*s%s/\n",depth,"",entry->d_name); 50 dfsFolder(entry->d_name,depth+4); 51 } else { 52 string filename = entry->d_name; 53 54 printf("%*s%s\n",depth,"",entry->d_name); 55 } 56 } 57 chdir(".."); 58 closedir(dp); 59 #endif 61 }

chdir 是C語言中的一個(gè)系統(tǒng)調(diào)用函數(shù)(同cd),其中對(duì)目錄"."及".."進(jìn)行特殊的判斷,因?yàn)?#34;."是表示當(dāng)前目錄,".."表示父目錄。

如果不進(jìn)行特殊判定的話則進(jìn)入死循環(huán)。

一個(gè)很簡單的功能,希望可以幫助得到需要的同學(xué)們~

轉(zhuǎn)載于:https://www.cnblogs.com/tickdream/p/3315726.html

總結(jié)

以上是生活随笔為你收集整理的cocos2d-x 实现跨平台的目录遍历的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。