cocos2d-x CSV文件读取 (Excel生成csv文件)

本文介绍了如何使用Cocos2d-x引擎将CSV文件转换并解析为Excel格式,并通过CCSVParse类实现数据读取与展示,最终在游戏场景中进行数据可视化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、准备素材


      1.EXCEL表:内容如下




      2.EXCEL表转换为csv文件:方法很多,网上搜索就有。

 但要注意:文件保存为UTF-8格式的(否则中文显示乱码)


二、实现代码:


1.操作csv方法类为CCSVParse

CCSVParse.h

#ifndef __cocos2d_x_Excel__CCSVParse__
#define __cocos2d_x_Excel__CCSVParse__

#include <iostream>
#include "cocos2d.h"
#include <vector>
using namespace std;
USING_NS_CC;

class CCSVParse
{
public:
//    CCSVParse(void);
    ~CCSVParse(void);
    CCSVParse(istream& fin=cin, string sep=","):
    fieldsep(sep),
    cols(0)
    {
        
    }
    
    //用以存储数据
    std::vector<std::vector<std::string> > data;
    bool openFile(const char* fileName);
    const char* getData(unsigned int rows, unsigned int cols);
    int findColsData(int cols, const char* value);
    
    inline int getCols(){return cols;}
    inline int getRows(){return data.size();}
    
private:
    string        fieldsep;
    int            cols;
    
    void StringSplit(const string& str, vector<string>& tokens, const char& delimiters);
    void split(vector<string>& field, string line);
    int advplain(const string& line, string& fld, int);
    int advquoted(const string& line, string& fld, int);
    
    
};

#endif /* defined(__cocos2d_x_Excel__CCSVParse__) */

CCSVParse.cpp

#include "CCSVParse.h"

//CCSVParse::CCSVParse(void)
//{
//    
//}

CCSVParse::~CCSVParse(void)
{
    
}

void CCSVParse::StringSplit( const string& str, vector<string>& tokens, const char& delimiters )
{
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    string::size_type pos = str.find_first_of(delimiters, lastPos);
    while (string::npos != pos || string::npos != lastPos)
    {
        tokens.push_back(str.substr(lastPos, pos-lastPos));
        lastPos = str.find_first_not_of(delimiters, pos);
        pos = str.find_first_of(delimiters, lastPos);
    }
}


void CCSVParse::split( vector<string>& field, string line )
{
    string fld;
    unsigned int i,j=0;
    
    if( line.length() ==0 )
        return;
    i=0;
    
    do
    {
        if(j<line.length() && line[i]=='"')
            j = advquoted(line, fld, ++i);
        else
            j = advplain(line, fld, i);
        
        field.push_back(fld);
        i = j+1;
    } while (j<line.length());
}



int CCSVParse::advplain( const string& s, string& fld, int i)
{
    unsigned int j;
    j = s.find_first_of(fieldsep, i);
    if(j>s.length())
        j=s.length();
    fld = string(s,i,j-i);
    return j;
}

int CCSVParse::advquoted( const string& s, string& fld, int i)
{
    unsigned int j;
    fld = "";
    for (j=i; j<s.length(); ++j)
    {
        if(s[j]=='"' && s[++j]!='"')
        {
            unsigned int k = s.find_first_of(fieldsep, j);
            if(k>s.length())
                k = s.length();
            for(k-=j; k-->0;)
                fld += s[j++];
            break;
        }
        fld += s[j];
    }
    return j;
}

//解析 CVS 文件
bool CCSVParse::openFile( const char* fileName )
{
    string pathKey = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(fileName);
    unsigned char* pBuffer = NULL;
    unsigned long bufferSize = 0;
    pBuffer = CCFileUtils::sharedFileUtils()->getFileData(pathKey.c_str(), "r", &bufferSize);
    
    string s = (char*)pBuffer;
    string str = s.substr(0,bufferSize);
    
    vector<string> line;
    StringSplit(str, line, '\n');
    for(unsigned int i=0; i<line.size(); ++i)
    {
        vector<string> field;
        split(field, line[i]);
        data.push_back(field);
        cols = max(cols, (int)field.size());
    }
    
    return true;
}

//获取指定行列的数据
const char* CCSVParse::getData(unsigned int rows, unsigned int cols )
{
    if (rows<0 || rows>=data.size() || cols<0 || cols>=data[rows].size())
    {
        return "";
    }
    return data[rows][cols].c_str();
}

//获取指定数据的列下标
int CCSVParse::findColsData( int cols, const char* value )
{
    for (unsigned int i=0; i<data.size(); ++i)
    {
        if(strcmp(getData(i,cols),value)==0)
            return i;
    }
    return -1;
}

      在init内添加如下代码:

    csvFile->openFile("testExcel.csv");
    for (int i=0; i<csvFile->getCols(); ++i)
    {
        string strLine = "";
        for(int j=0; j<csvFile->getRows(); ++j)
        {
            strLine += csvFile->getData(i,j);
            strLine += ",";
        }
        CCLabelTTF* pLab = CCLabelTTF::create(strLine.c_str(),"Arial",20);
        pLab->setColor(ccc3(255, 0, 0));
        pLab->setPosition(ccp(size.width/2,size.height-150-i*30));
        this->addChild(pLab,2);
    }
    delete csvFile;

      运行效果如下:




引用博文:http://www.cnblogs.com/MrGreen/archive/2013/09/03/3295662.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热血枫叶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值