How to save/read file on different platforms

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.
  • How to save/read file on different platforms
  • Rebuild and run

You will see the log in eclipse like:

How to save/read file on different platforms

Also, you can see the file content:

How to save/read file on different platforms

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.
  • How to save/read file on different platforms
  • Rebuild and run

You will see the log in eclipse like:

How to save/read file on different platforms

Also, you can see the file content:

How to save/read file on different platforms

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.
  • How to save/read file on different platforms
  • Rebuild and run

You will see the log in eclipse like:

How to save/read file on different platforms

Also, you can see the file content:

How to save/read file on different platforms

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.

上一篇:网页端打开手机上的app


下一篇:JSPServlet精华笔记