xcode 使用 COCOS2D-X 进行文件 I/O
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12171445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File I/O using COCOS2D-X
提问by alxcyl
I'm trying to load up a Comma Separated file called POSDATA.GAMEDATA
. I've looked up several places on the internet and it turns out I need to do some tweaking and / or a different class.
我正在尝试加载一个名为POSDATA.GAMEDATA
. 我在互联网上查了几个地方,结果我需要做一些调整和/或不同的类。
I tried using ifstream
. However, it cannot open the file. Xcode 4.3.2 cannot seem to find my POSDATA.GAMEDATA
file. I also tried to make the file using ofstream
but when I use open()
in both cases, the file is not opened.
我尝试使用ifstream
. 但是,它无法打开文件。Xcode 4.3.2 似乎找不到我的POSDATA.GAMEDATA
文件。我也尝试使用该文件,ofstream
但是当我open()
在这两种情况下使用时,该文件都没有打开。
My code is something like this:
我的代码是这样的:
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;
}
I've seen CCFileUtils
but I can't seem to get how to use it.
我见过,CCFileUtils
但我似乎不知道如何使用它。
EDIT: I've tried supplying the absolute path ( /Users/LanceGray/Documents/LanceDev/COCOS2DX/cocos2dx/TestGame/Data/POSDATA.GAMEDATA
) and it worked. However, I cannot do this since the game is supposed to be used in iOS devices and Android, so the path is not always the same on each device. Any help will be grealy appreciated.
编辑:我试过提供绝对路径 ( /Users/LanceGray/Documents/LanceDev/COCOS2DX/cocos2dx/TestGame/Data/POSDATA.GAMEDATA
) 并且它有效。但是,我不能这样做,因为该游戏应该在 iOS 设备和 Android 中使用,因此每个设备上的路径并不总是相同的。任何帮助将不胜感激。
回答by alxcyl
I got working by using CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( "POSDATA.GAMEDATA" );
我开始使用 CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( "POSDATA.GAMEDATA" );
A more detailed explanation:
更详细的解释:
- Add the files that you need in the Project by going to the Project Tree on the left and
Right-click -> Add Files
. What I did was I added a new folder calledData
on the same level as theResources
andClasses
folders and placed myPOSDATA.GAMEDATA
file there. In Xcode, I added a new group and added that file in that group. - Then I used
ifstream
to open the file. - When opening the file, use
CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( )
to get the absolute path of the file. Supply the file name on thefullPathFromRelativePath( )
as argument. - Try to run it and it should work fine.
- 通过转到左侧的项目树和 ,在项目中添加您需要的文件
Right-click -> Add Files
。我所做的是添加了一个Data
与Resources
和Classes
文件夹处于同一级别的新文件夹,并将我的POSDATA.GAMEDATA
文件放在那里。在 Xcode 中,我添加了一个新组并将该文件添加到该组中。 - 然后我用来
ifstream
打开文件。 - 打开文件时,使用
CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( )
获取文件的绝对路径。在fullPathFromRelativePath( )
as 参数中提供文件名。 - 尝试运行它,它应该可以正常工作。
A small example:
一个小例子:
// 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;
}
This class will load a file with the name pFileName
and place it on its member variable mFileContents
. ( Note that it should have a public get
function like vector< string > getFileContents( )
to access the mFileContents
because it is private
)
此类将加载具有该名称的文件pFileName
并将其放置在其成员变量中mFileContents
。(请注意,它应该有一个public get
类似于vector< string > getFileContents( )
访问的功能,mFileContents
因为它是private
)
EDIT: The above sample will work on iOS, however, it won't on Android devices. So to fix this, instead of using ifstream
, use CCFileUtils::sharedUtils( ) -> getFileData( )
instead. In conjunction with CCFileUtils::sharedUtils( ) -> fullPathFromRelativePath( )
, we will be able to achieve our goal of reading a plain text file that works on both iOS and Android.
编辑:上述示例适用于 iOS,但不适用于 Android 设备。所以要解决这个问题,不要使用ifstream
,CCFileUtils::sharedUtils( ) -> getFileData( )
而是使用。结合CCFileUtils::sharedUtils( ) -> fullPathFromRelativePath( )
,我们将能够实现读取适用于 iOS 和 Android 的纯文本文件的目标。
The FileReader
class would then be like this:
在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;
}
回答by m.ding
// 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);
回答by thphong
You can reference from Cocos's wiki Read/write file in cocos2d
你可以参考 Cocos 的 wiki Read/write file in cocos2d