gpt4 book ai didi

c++ - 使用 COCOS2D-X 的文件 I/O

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:05:03 24 4
gpt4 key购买 nike

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

我尝试使用 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;
}

我见过 CCFileUtils 但我似乎不知道如何使用它。

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

最佳答案

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

更详细的解释:

  1. 通过转到左侧的项目树和Right-click -> Add Files,在项目中添加您需要的文件。 .我所做的是添加了一个名为 Data 的新文件夹与 Resources 处于同一级别和 Classes文件夹并放置我的 POSDATA.GAMEDATA在那里归档。在 Xcode 中,我添加了一个新组并将该文件添加到该组中。
  2. 然后我用了ifstream打开文件。
  3. 打开文件时,使用CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( )获取文件的绝对路径。在 fullPathFromRelativePath( ) 上提供文件名作为论点。
  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 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;
}

关于c++ - 使用 COCOS2D-X 的文件 I/O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12171445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com