// 数据段 struct selectCardData_s { int id; int cardType; int rewardMultiple; double chance; };
std::string strTmp; std::string subStrTmp; std::string str; int pos = 0; int subPos1 = 0; int subPos2 = 0; Data fileData = FileUtils::getInstance()->getDataFromFile( FileUtils::getInstance()->fullPathForFilename( "data/SelectCard.csv" ) ); str.clear(); str.assign( (const char *)fileData.getBytes(), fileData.getSize() ); /* string 拆解 for( int i = 0; i < data.getSize(); i++ ) { pos = str.find( " ", i ); strTmp = str.substr( i, pos - i ); i = pos; } */
unsigned int y = 0;
for( unsigned int i = 0; i < str.size(); i++, y++ ) {
pos = str.find( " ", i ); strTmp = str.substr( i, pos - i ); // 拆数据,第一行不要 if( y != 0 ) { selectCardData_s *data = new selectCardData_s; subPos1 = 0; // subPos2 = strTmp.find( ",", subPos1 ); subStrTmp = strTmp.substr( subPos1, subPos2 - subPos1 ); data->id = atoi( subStrTmp.c_str() ); subPos1 = subPos2 + 1; // 需要自加,跳过分割符号 // subPos2 = strTmp.find( ",", subPos1 ); subStrTmp = strTmp.substr( subPos1, subPos2 - subPos1 ); data->cardType = atoi( subStrTmp.c_str() ); subPos1 = subPos2 + 1; // subPos2 = strTmp.find( ",", subPos1 ); subStrTmp = strTmp.substr( subPos1, subPos2 - subPos1 ); data->rewardMultiple = atoi( subStrTmp.c_str() ); subPos1 = subPos2 + 1; // subPos2 = strTmp.find( ",", subPos1 ); subStrTmp = strTmp.substr( subPos1, subPos2 - subPos1 ); data->chance = atof( subStrTmp.c_str() ); subPos1 = subPos2 + 1; // 记录数据 dataMap[ data->id ] = data; } if( pos == - 1 ) { break; } i = pos; }
拆分的CSV里面的数据格式为:
id,cardType,rewardMultiple,chance 1,5,10,测试
所以从第二行开始才是真实的数据