Traversing directories cross platform with Cocos2d

Traversing directories cross platform with Cocos2d
0.0 0


Is there a good cross-platform way traverse directory structures? There is a way in Linux, a way in Windows, and a way in iOS, but does Cocos2d-x address this? If not, are there some methods that the community could recommend to get around this problem?

Excellent work on Cocos2d-x by the way. It is absolutely an amazing and fun experience writing a cross-platform game that has an API as easy to use as the one used in Cocos2d-x. Hopefully other API authors take note of the excellent work done here!


Thanks for your admiration :slight_smile:
For creating cross-platform folder structure, this blog is recommended How to create an iPhone and Android Cocos2d-x hybrid project


Hey Walzer,

Thanks for your quick reply. I was looking more for a way to query directories using Cocos2d-x. fopen, fread, ect. are all cross-platform, but directories don’t seem to have a similar place in C++. On a win32 machine, one would use FindFirstFile, FindNextFile, ect. POSIX works on linux boxes and has a DIR class similar to the FILE class. iOS has NSFileManager. Is there a good cross-platform implementation that rolls all this into a single wrapper? Are there plans to do so for Cocos2d-x, or would it be faster to implement it on my own?


Oh, sorry, I misunderstanded.
Currently I don’t know if there’s any cross-platform solution for foder operations. What a pity.


Hey Walzer,

There might be a fairly easy fix that could be incorporated into cocos2d-x. It looks like a header <dirent.h> works for android, and there is a free win32 implementation that can be found at the bottom of the wiki page here: Can someone check to see if this is included and would work easily for iOS? If so, perhaps <dirent.h> for win32 could be added to the win32 projects and this could be all wrapped up in the CCFileUtils function?


Sorry to bring a thread back to life, but the <dirent.h> that can be found on the wiki page does work really well for win32 applications.


Awesome! I just tested to call methods in dirent.h, it can successfully be built on ios 5.0 sdk (but haven’t run yet). It’s a good start.


Feature #849 created for this. But I wanna to know, when you need this feature? Could you please describe a user case for it?


I’m making a sprite based wargame and would like users to be able to add content (campaigns, units, unit stats, etc.) easily through an XML type language. To keep user elements discrete and separate, the rule will be that all user added be able to run from the content in its own folder so it can be easily added and deleted. A folder navigation element is critical in a case like this. I’m using the CCFileUtils::getWriteablePath() call to get the “root” file path, and folders will be added under a Campaignsdirector.


I’m sorry for reviving this thread but I would also like this feature. If would be great if CCFileUtils had a static method listDirectoryContents. In a game I made for Android in java I used functions like that to list the contents of the directory containing the game’s maps. That was a very flexible way of building the maps list without having to do anything but drop another map file in the maps directory of the game. It would also be create for user generated content or downloaded maps like Austin suggested.

I tried using dirent.h for this but it seems it can’t list directories under the resources on Android. It worked fine on iOS though. I posted about it here:

Thanks for a great library! :slight_smile: I’m porting said game to cocos2dx now for platform independency! :smiley:


This definitely a good to have feature. Any progress? Thanks


Add any files into application folders and remove them by application itself and save this changes in db.
So application will ever know what files exists in its folders… Platform does not matter :slight_smile:


bagus flyer wrote:

This definitely a good to have feature. Any progress? Thanks

This is still not implemented.


reviving the thread again !!
is this implemented in cocos2d-x v3.14 or not?