6

というカンマ区切りファイルを読み込もうとしていますPOSDATA.GAMEDATA。インターネットでいくつかの場所を調べたところ、微調整や別のクラスを行う必要があることがわかりました。

使ってみifstreamました。ただし、ファイルを開くことはできません。POSDATA.GAMEDATAXcode 4.3.2 がファイルを見つけられないようです。も使用してファイルを作成しようとしました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;
}

見ましCCFileUtilsたが、使い方がわかりません。

編集:絶対パス()を指定しようとしましたが、うまくいきました/Users/LanceGray/Documents/LanceDev/COCOS2DX/cocos2dx/TestGame/Data/POSDATA.GAMEDATA。ただし、ゲームはiOSデバイスとAndroidで使用されることになっているため、これはできません。そのため、各デバイスでパスが常に同じであるとは限りません. どんな助けでも大歓迎です。

4

3 に答える 3

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;
}

このクラスは、その名前のファイルをロードし、pFileNameそのメンバー変数に配置しますmFileContents。( ですのでにアクセスするpublic getような機能が必要なので注意してください)vector< string > getFileContents( )mFileContentsprivate

編集: 上記のサンプルは 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;
}
于 2012-08-29T06:33:56.517 に答える
2
// For versions less than v2.0.1
// The version I am using is 0.12.0
unsigned long fileSize = 0;
char* pBuffer = CCFileUltils::getFileData("relative_path","r",&fileSize);
CCLOG("Data is %s",pBuffer);
于 2012-08-29T05:45:58.870 に答える
0

Cocos の wiki から参照でき ます cocos2d の読み取り/書き込みファイル

于 2015-02-01T04:44:35.293 に答える