• CSV用C++拆分数据(CSV必须是UTF-8格式,引擎是cocos2d-x 3.3)


    // 数据段 
    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,测试

    所以从第二行开始才是真实的数据

  • 相关阅读:
    后台向前台输出 换行“ ”
    Razor视图引擎输出没有编码的 Html 字符串
    C#之delegate
    C#之Action
    What's New in C# 6.0
    Git 放弃修改
    Spring.Net+Nhibernate
    nmap扫描ip段
    nginx多个if条件并且查询
    查看哪些redis命令拖慢了redis
  • 原文地址:https://www.cnblogs.com/Colored-Mr/p/4989163.html
Copyright © 2020-2023  润新知