使用COCOS2D-X进行文件I/O.

alx*_*cyl 6 c++ xcode cocos2d-x

我正在尝试加载一个名为的逗号分隔文件POSDATA.GAMEDATA.我在互联网上查了几个地方,结果发现我需要做一些调整和/或不同的课程.

我试过用ifstream.但是,它无法打开文件.Xcode 4.3.2似乎找不到我的POSDATA.GAMEDATA文件.我也尝试使用该文件,ofstream但是当我open()在两种情况下使用时,文件都没有打开.

我的代码是这样的:

using namespace std;
void FileLoader::loadFile( string p_WhichFile ) {
   // Local Variables
   string thisLine;

   // Open POSDATA.GAMEDATA
   ifstream dataStream;
   dataStream.open( p_WhichFile.c_str( ) );

   // Check if file is opened
   if ( !dataStream ) {
      cerr << "[ ERROR ] Cannot load file:" << p_WhichFile.c_str( ) << endl;
      exit( 1 );
   }

   // Get lines of strings
   while ( getline( dataStream, thisLine ) ) {
      fileContents.push_back( thisLine ); // fileContents is a vector< string > object
   }

   dataStream.close( );
   cout << "[ NOTICE ] Finished reading file" << p_WhichFile << endl;
}
Run Code Online (Sandbox Code Playgroud)

我见过CCFileUtils但似乎无法使用它.

编辑:我已经尝试提供绝对路径(/Users/LanceGray/Documents/LanceDev/COCOS2DX/cocos2dx/TestGame/Data/POSDATA.GAMEDATA),它工作.但是,我不能这样做,因为游戏应该在iOS设备和Android中使用,所以每个设备上的路径并不总是相同.任何帮助将非常感激.

alx*_*cyl 17

我通过使用工作 CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( "POSDATA.GAMEDATA" );

更详细的解释:

  1. 通过转到左侧的项目树,添加项目中需要的文件Right-click -> Add Files.我做的是我添加了一个与文件夹Data在同一级别调用的新文件夹,并将我的文件放在那里.在Xcode中,我添加了一个新组,并在该组中添加了该文件.ResourcesClassesPOSDATA.GAMEDATA
  2. 然后我习惯ifstream打开文件.
  3. 打开文件时,用于CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( )获取文件的绝对路径.在fullPathFromRelativePath( )as参数上提供文件名.
  4. 尝试运行它,它应该工作正常.

一个小例子:

// 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 <fstream>
#include "cocos2d.h"

using namespace cocos2d;
using namespace std;

FileReader::FileReader( string pFileName, char pMode ) {
   // Create input file stream
   ifstream inputStream;
   string thisLine;

   // Open file
   inputStream.open( CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( pFileName ).c_str( ) );

   // Check if it is open
   if ( !inputStream.is_open( ) ) {
      cerr << "[ ERROR ] Cannot open file: " << pFileName.c_str( ) << endl;
      exit( 1 );
   }

   while ( getline( inputStream, thisLine ) ) {
      // Put all lines in vector
      mFileContents.push_back( thisLine );
   }

   inputStream.close( );
   cout << "[ NOTICE ] Finished opening file: " << pFileName.c_str( ) << endl;
}
Run Code Online (Sandbox Code Playgroud)

此类将加载具有名称的文件pFileName并将其放在其成员变量上mFileContents.(注意它应该有一个public getvector< string > getFileContents( )访问mFileContents它的功能,因为它是private)

编辑:上面的示例将适用于iOS,但它不会在Android设备上.所以要解决这个问题,而不是使用ifstream,CCFileUtils::sharedUtils( ) -> getFileData( )而是使用.与此同时CCFileUtils::sharedUtils( ) -> fullPathFromRelativePath( ),我们将能够实现读取适用于iOS和Android的纯文本文件的目标.

这个FileReader课程就像这样:

// FileReader.cpp
#include "FileReader.h"
#include <fstream>
#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;
}
Run Code Online (Sandbox Code Playgroud)