You can use standard c functions, such as fopen, fwrite, to save and read file on different platforms. Of cource, you can use other standard functions to operate with a file. The difficulty is the path to save or read a file.
This article modifies the "HelloWorld" example to show you how to save/read file on different platforms.
Now, CCFileUtils::getWriteablePath() returns a writeable path. You can use it to get the writeable path.
1. Run example on different platforms
1.1 Android
- Copy FileOperation.h and FileOperation.cpp into cocos2d-x/HelloWorld.
- Invoke FileOperation::saveFile() and FileOperation::readFile() in HelloWorld::init().
- Modify cocos2d-x/HelloWorld/android/jni/helloworld/Android.mk.
- Rebuild and run
You will see the log in eclipse like:
Also, you can see the file content:
1.2 iOS
- Copy FileOperation.h and FileOperation.mm into cocos2d-x/HelloWorld.
- Invoke FileOperation::saveFile() and FileOperation::readFile() in HelloWorld::init().
- Add FileOperation.h and FileOperation into project file.
- Rebuild and run
You will see the log in eclipse like:
Also, you can see the file content:
Please reference to the apple document:
iOS Aplication Programming Guide - File & the File system
CHN version
1.3 Windows
- Copy FileOperation.h and FileOperation.cpp into cocos2d-x/HelloWorld.
- Invoke FileOperation::saveFile() and FileOperation::readFile() in HelloWorld::init().
- Add FileOperation.h and FileOperation into project file.
- Rebuild and run
You will see the log in eclipse like:
Also, you can see the file content:
2. Summary
This article just gives a guide of how to save/read file on different platforms. It is not the standard method,
just a workable method.