Proper File I/O for cross platform

Hi,

I was wondering how simple file operations could be done for saving games for example,

The official guide (http://www.cocos2d-x.org/wiki/How_to_read_and_write_file_on_different_platforms) only supports iOS/Android and does not even support if the file exists?

Any help?
Thanks

Personally I use the standard C FILE/fopen/fread/fwrite/fclose/fseek/fflush… works on all platforms. I’ve been using it for 20+ years!

Check out the FileUtils class too:
http://www.cocos2d-x.org/reference/native-cpp/V3.2/dc/d69/classcocos2d_1_1_file_utils.html

std::string fullPath = CCFileUtils::sharedFileUtils()->getWriteablePath(); // Get the current writable path

1 Like

I’m using standard c++ i/o, and using FileUtils class like GMTDev suggest. For ex:

string path = FileUtils::getInstance()->getWritablePath() + “\\save_data.dat”;

ifstream readData(path, ios::binary);
    if (readData.is_open())
    {
        readData.read((char*)&_playerData, sizeof(PlayerData));
        readData.close();
    }

p/s: in my example, PlayerData is a struct type

1 Like