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.

  • 201311230751.jpg
  • Rebuild and run

You will see the log in eclipse like:


201311230751.jpg

Also, you can see the file content:


201311230751.jpg

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.

  • 201311230751.jpg
  • Rebuild and run

You will see the log in eclipse like:


201311230752.jpg

Also, you can see the file content:


201311230752.jpg

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.

  • 201311230752.jpg
  • Rebuild and run

You will see the log in eclipse like:


201311230752.jpg

Also, you can see the file content:


201311230752.jpg

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.


原文地址:https://www.cnblogs.com/yssgyw/p/3438593.html