私は使って仕事をしましたCCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( "POSDATA.GAMEDATA" );
より詳細な説明:
- 左側のプロジェクト ツリーに移動して、必要なファイルをプロジェクトに追加します
Right-click -> Add Files
。私がしたことは、およびフォルダーとData
同じレベルにあるという名前の新しいフォルダーを追加し、そこにファイルを配置することでした。Xcode で、新しいグループを追加し、そのグループにそのファイルを追加しました。Resources
Classes
POSDATA.GAMEDATA
- それから私
ifstream
はファイルを開いていました。
- ファイルを開くときに、ファイル
CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( )
の絶対パスを取得するために使用します。fullPathFromRelativePath( )
as 引数にファイル名を指定します。
- 実行してみてください。正常に動作するはずです。
小さな例:
// 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( )
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;
}