File I/O in Cocos2d-x 2.0.1

I am using istream to open files and CCFileUtils::sharedUtils( ) -> fullPathFromRelativePath( ) to get the file path. It works fine on the iOS Simulator but when I test it on an Android Device, it doesn’t work. A black screen is shown and the app shuts down.

Here’s some of my code:

// FileLoader.cpp
FileLoader::FileLoader( string pFileName ) {
   ifstream dataStream;
   string thisLine;

   // Example: pFileName = "DATAFILE.GAMEDATA"
   // - It is added in Xcode ( v4.3.2 ) in a folder called "Data"
   // - There is also an alias pointing to the "Data" folder in Eclipse
   dataStream.open( CCFileUtils::sharedFileUtils() -> fullPathFromRelativePath( pFileName ).c_str( ) );

   if ( !dataStream ) {
      exit( 1 );
   }

   while ( getline( dataStream, thisLine ) ) {
      mContents.push_back( thisLine ); // mContents is a vector< string> object
   }

   dataStream.close( );
}

In the iOS Simulator, it works fine and the game runs perfectly but in the Android Device, it does not and only a black screen appears. Also, will it work on an iOS device? I don’t have it right now so I can’t test, but I assume it would work since it worked in the simulator. Thanks in advance.

in android device, make a copy of file data into android writable path,and read then read it.
it maybe solve your problem,but not best way.
can anyone solve it .

#if (CC_TARGET_PLATFORM CC_PLATFORM_IOS)
ifstream in(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(fileName));
#elif (CC_TARGET_PLATFORM CC_PLATFORM_ANDROID)
std::string writablePath = cocos2d::CCFileUtils::sharedFileUtils()->getWriteablePath();
writablePath = writablePath + fileName;
FILE fp = fopen, “r”);
if
{
fclose;
}
else {
const char
fullPath = CCFileUtils::fullPathFromRelativePath(fileName);
CCFileData data(fullPath, “rb”);
if (data.getSize())
{
FILE *desFile = fopen(writablePath.c_str(), “w+”);
fwrite(data.getBuffer(),data.getSize(),1,desFile);
fclose(desFile);
}
else {
debug_Printf(“copy file to writeable path failed”);
return mKey;
}
}

ifstream in(writablePath.c_str());
#endif

if(!in)
{
std::cout << “Open file failed” <<std::endl;
debug_Printf(“Open file failed”);
return mComment;
}

I did a work around for this. Instead of using ifstream I used CCFileUtils to get it.

// FileReader.h
#include "cocos2d.h"

using namespace std;
using namespace cocos2d;

class FileReader {
private:
   vector< string > mFileContents;

public:
   FileReader( string pFileName, char pMode = 'r' );
};

// FileReader.cpp
#include "FileReader.h"
#include 
#include "cocos2d.h"

using namespace cocos2d;
using namespace std;

FileReader::FileReader( string pFileName, char pMode ) {
   // Initialize variables needed
   unsigned long fileSize = 0;
   unsigned char * fileContents = NULL;
   string thisLine, result, fullPath, contents;

   // Get absolute path of file
   fullPath = CCFileUtils::sharedFileUtils( ) -> fullPathFromRelativePath( pFileName.c_str( ) );

   // Get data of file
   fileContents = CCFileUtils::sharedFileUtils( ) -> getFileData( fullPath.c_str( ) , "r", &fileSize );
   contents.append( ( char * ) fileContents );

   // Create a string stream so that we can use getline( ) on it
   istringstream fileStringStream( contents );

   // Get file contents line by line
   while ( getline( fileStringStream, thisLine ) ) {
      // Put all lines in vector
      mFileContents.push_back( thisLine );
   }

   // After this, mFileContents will have an extra entry and will have the value '\x04'.
   // We should remove this by popping it out the vector.
   mFileContents.pop_back( );

   // Delete buffer created by fileContents. This part is required.
   if ( fileContents ) {
      delete[ ] fileContents;
      fileContents = NULL;
   }

   // For testing purposes
   cout << "[ NOTICE ] Finished opening file: " << pFileName.c_str( ) << endl;
}

I got it working but I encountered some problems along the way.