cocos2d-x转android读文件注意!
遇到的问题:有时候读完最后一行还会再读一行空的
解决方法:删除最后一行,再继续判断读到最后一行是否是字母D(以防止多读一行的情况下)
//从文件中读取数据 第一行为注释 需去除
CCMutableArray<CCString*>* pathArray = readFileDataByLine(PATH_FILE_NAME);
pathArray->removeObjectAtIndex(0);
pathArray->removeLastObject();
//如果每行开始读到的不是1-9 则删除配置文件最后一行
CCString *stringTmp = pathArray->getLastObject();
if (!stringTmp->isEmpty())
{
char s = *(stringTmp->m_sString.begin());
//ASCII码判断 《在文件的最后一行加入D 用UltraEdit编辑》
if (s == 68)
{
pathArray->removeLastObject();
CCLog("~~pathArray->removeLastObject() finish");
}
CCLog("~~path.csv文件ASCII~~%c", s);
}
或者是readFileDataByLine函数读行的代码问题,大家若发现忘改正!!
static CCMutableArray<CCString*>* readFileDataByLine(const char * fileName, bool isBinary = true)
{
CCMutableArray<CCString*>* array = newCCMutableArray<CCString*>();
unsigned long bufferSize = 0;
char* mode = "wt+";
if(isBinary)
{
mode = "r";
}
unsigned char* content = cocos2d::CCFileUtils::getFileData(fileName,mode,&bufferSize);
if (bufferSize == 0)
{
array->autorelease();
return array;
}
vector<string> v = split((const char*)content,"\n");
CCString* lineContent = NULL;
for (std::vector<string>::iterator iter = v.begin(); iter != v.end(); ++iter)
{
lineContent = new CCString((*iter).c_str());
array->addObject(lineContent);
lineContent->release();
}
array->autorelease();
return array;
}