Reading and writing to file cocos2dx

Reading and writing to file cocos2dx
0.0 0



I read this page about reading and writing to a file.

I’m more confused now than before I read it.

It suggests that you use CCFileUtils::getWriteablePath() to get the path, I assume this is the correct path for all platforms.

It then gives examples that are different for the 3 platforms that I use (IOS, Android and Win32).

For IOS it suggests creating a FileOperation class in the ios folder (not classes) (also, the FileOperation.h is missing in screenshot).
For both Android and Win32 it suggests creating the class in the Classes folder (which makes more sense).

If you look in the example classes, FileOperation has its own getFilePath function, this does not use CCFileUtils::getWriteablePath.

The write and read functions are the same in the .mm and .cpp version of the files, only getFilePath is different.

can I just do this:-

void MyClass::saveFile(string path)
    FILE *fp = fopen(path.c_str(), "w");

    if (! fp)
        CCLOG("can not create file %s", path.c_str());

    fputs("file example", fp);

void MyClass::readFile(string path)
    FILE *fp = fopen(path.c_str(), "r");
    char buf[50] = {0};

    if (! fp)
        CCLOG("can not open file %s", path.c_str());

    fgets(buf, 50, fp);
    CCLOG("read content %s", buf);


And then call readFile and writeFile passing in CCFileUtils::getWriteablePath() + “/myfilename.txt”

Will this work on all platforms?
Should I use CCFileUtils::getWriteablePath() when reading a file?

If the wiki page is giving bad advise, can it be updated?

Save level editor file to be loaded on an android device

OK, so this seems to be working on android device, win32 and iPhone.

std::string path = CCFileUtils::sharedFileUtils()->getWriteablePath() + "\\file.ext";
FILE* f = fopen(path.c_str(), "rb");

if(f == NULL){
  //do stuff to create the empty data
  int count;
  fread(&count, sizeof(int), 1, f);
  for(int i = 0; i < count; i++)
    my_struct p;
    fread(&p, sizeof(my_struct), 1, f);
    vector.push_back(p);  //vector of my_struct

and for saving:-

std::string path = CCFileUtils::sharedFileUtils()->getWriteablePath() + "\\file.ext";
FILE* f = fopen(path.c_str(), "wb");

int size = vector.size(); //number of items to save
fwrite(&size, sizeof(int), 1, f);
for(unsigned int i = 0; i


what exactly is this my_struct p and sizeof(my_struct) all about?
What I am trying to do is download a file from my web server and save it to a file.
The text gets printed out correctly:

    std::vector *buffer = response->getResponseData();

    for (unsigned int i = 0; i < buffer->size(); i++)
        printf("%c", (*buffer)[i]);

I’d like to know how I can write this to a file instead of just printing it out.
Also, how do I save .png or .lua files for example.
Thanks for your little tutorial though, I hope this is what I was looking for :slight_smile:


Hi,in my case, my_struct is a custom structure I created to hold my level info.
it contains some ints and strings.

sizeof(my_struct) just tells the fwrite function how big each block of data is.

In your case, you are just using a vector of char which does not make a lot of sense to me.

You could just write the whole string out in 1 go, no need for the loop.


ok thanks. got it to work now :slight_smile:


Adam Reed,
I checked on my end (cocos2d-x v.2.0.3) that CCFileUtils::sharedFileUtils()->getWriteablePath() returns Caches folder. Is Caches folder suitable for saving save-file? I believe it should be Documents folder as seen in

Am I missing anything?


Sorry, I checked in the cocos2d-x source, and it called NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);. So it is like it showed to me.

As checked on, it seems like I have to be more careful on choosing path to save. Thanks.