文件、导出-在Mac系统下Excel转csv文件中文乱码问题解决-by小雨

首先声明,我是一个菜鸟。一下文章中涌现技术误导情况盖不负责

    导出式方

    问题的原因是编码式方不同形成的,解决问题要需借助一个具工

    Numbers,

    载下址地: http://soft.macx.cn/5144.htm

    安装成完后用Numbers打开Excel文档;

    在最上方点击:同享->导出

    涌现

    在这里择选csv,编码式格一定要择选UTF-8,然后点击下一步导出;

    

    剖析式方

    在Mac下导出的csv是以逗号分割的;

    面下是封装的剖析类

    CSVParse.h

#include <stdio.h>
#include <vector>

using namespace std;

class CSVParse {
    
public:
    int row;
    int col;
    
public:
    //调用剖析
    CSVParse(const char* fileName, string sep = ",");
    ~CSVParse();
    
private:
    /**
     *  分隔符
     */
    string m_fieldsep;
    
    /**
     *  容器,存储从CSV里取读出来的数据
     */
    vector<vector<string> > m_data;
    
private:
    void split(vector<string>& field,string line);
    int advplain(const string& line, string& fld, int);
    int advquoted(const string& line, string& fld, int);
    
    /**
     *  除删替换特定字符
     */
    void deleteChar(std::string* str);
    
public:
    /**
     *  打开文件
     */
    bool openFile(const char* fileName);
    
    /**
     *  取数据
     */
    const char* getData(int m,int n);
};

    CSVParse.cpp

    

#include "cocos2d.h"
#include "CSVParse.h"

CSVParse::CSVParse(const char* fileName, string sep)
:m_fieldsep(sep)
{
    openFile(fileName);
}

CSVParse::~CSVParse()
{
    for (int i=0; i<m_data.size(); i++) {
        m_data[i].clear();
    }
    m_data.clear();
}

void CSVParse::split(vector<string>& field,string line)
{
    string fld;
    int i, j;
    
    if (line.length() == 0)
        return ;
    i = 0;
    
    do {
        if (i < 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 CSVParse::advquoted(const string& s, string& fld, int i)
{
    int j;
    
    fld = "";
    for (j = i; j < s.length(); j++)
    {
        if (s[j] == '"' && s[++j] != '"')
        {
            int k = s.find_first_of(m_fieldsep, j);
            if (k > s.length())
                k = s.length();
            for (k -= j; k-- > 0; )
                fld += s[j++];
            break;
        }
        fld += s[j];
    }
    return j;
}

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


const char* CSVParse::getData(int m,int n)
{
    if ( m<0 || m>=m_data.size() || n<0 || n>=m_data[m].size() ) {
        return "";
    }
    
    //printf("%d,%d,%s\n", m, n, m_data[m][n].c_str());
    
    return m_data[m][n].c_str();
}

bool CSVParse::openFile(const char* fileName)
{
    //取获全径路
    string pathKey = cocos2d::CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(fileName);
    
    //打开文件
    //    size_t size = 0;
    FILE *fp = fopen(pathKey.c_str(), "r");
    if( !fp ) {
        CCLOG("打开文件%s失败", pathKey.c_str());
        return false;
    }
    
    //取获文件字节数
    //    fseek(fp, 0, SEEK_END);
    //    size = ftell(fp);
    //    fseek(fp, 0, SEEK_SET);
    
    //取读内容
    //    unsigned char* out = (unsigned char*)malloc(size);
    //    size_t read = fread(out, 1, size, fp);
    //    if( read != size ) {
    //        CCLOG("取读文件%s失败", pathKey.c_str());
    //        free(out);
    //        *out = NULL;
    //        return false;
    //    }
    
    char tmpChar[2048] = {0};
    string s;
    
    //去失落\r
    int lineIndex = 0;
    
    //取读第一行
    fgets(tmpChar, 2048, fp);
    while( strlen(tmpChar) > 0 )
    {
        s = tmpChar;
        //printf("%d = %s", lineIndex, tmpChar);
        
        //除删和替换失落余多字符
        deleteChar(&s);
        
        //拆分失落文本
        std::vector<string> field;
        split(field, s);
        
        //第一行和第一列是无用数据,所以不存.
        if(lineIndex > 0){
            field.erase(field.begin());
            m_data.push_back(field);
        }
        lineIndex++;
        
        //取读下一行
        tmpChar[0] = '\0';
        fgets(tmpChar, 2048, fp);
    }
    
    row = m_data.size();
    col = m_data[0].size();
    
    //试测,输出内容
//    for (int i=0; i<m_data.size(); i++) {
//        for (int k=0; k<m_data[i].size(); k++) {
//            CCLOG("--------->%s",getData(i, k));
//        }
//    }
    
    fclose(fp);
    
    return true;
}


void CSVParse::deleteChar(std::string* str){
    string::iterator it;
    int index = 0;
    for (; index < str->size(); )
    {
        it = str->begin()+index;
        if ( *it == '\r' || *it == '\n' )
        {
            str->erase(it);
        } 
        else{
            index++;
        }
    }    
}

    剖析类的用法

    langFile为要剖析的文件名

    CSVParse* csv = newCSVParse(langFile);

    失失落CSVParse对象后可以调用

    const char* getData(int m,int n);

    传入行和列可以失失落想要的数据

    

    

    代码载下:

    http://download.csdn.net/detail/dingkun520wy/5263136

    

    

文章结束给大家分享下程序员的一些笑话语录: 真正的程序员喜欢兼卖爆米花,他们利用CPU散发出的热量做爆米花,可以根据米花爆裂的速度听出正在运行什么程序。

posted @ 2013-04-16 13:34  坚固66  阅读(829)  评论(0编辑  收藏  举报