簡易csv解析

2022-03-11 22:19:24 字數 2073 閱讀 6774

遊戲開發中少不了配置資料,csv格式既方便匯出又方便使用,下面是簡單csv解析(字段資料中不能含有分隔符),以後會繼續完善,嘿嘿。

//

// ccsvparse.hpp

// cppalgorithm

//// created by xujw on 16/2/26.

//#ifndef ccsvparse_h

#define ccsvparse_h

#include

#include

#include

#include

class ccsvparse

; /*

分隔字串

str:要分隔的字串/檔案

seperator:分隔符

*/std::vector

splitstring(const

std::string &str,const

std::string &sparator);

std::string loadcsvfile(const

std::string &filename);

private:

//原始資料

std::vector

< std::vector

> _griddata;

};#endif /* ccsvparse_h */

//// ccsvparse.cpp

// cppalgorithm

//// created by xujw on 16/2/26.

//#include "ccsvparse.h"

ccsvparse::ccsvparse(){}

ccsvparse::~ccsvparse(){}

std::vector

ccsvparse::splitstring(const

std::string &str, const

std::string &separator)

//方便擷取最後一段資料

std::string datastr = str + separator;

size_t pos = datastr.find(separator);

size_t size = datastr.size();

while (pos != std::string::npos)

return resvec;

}std::string ccsvparse::loadcsvfile(const

std::string &filename)

fseek(pfile, 0, seek_end); //指標移動到檔案結尾

long len = ftell(pfile); //獲取檔案大小

char *pbuffer = new

char[len+1];

fseek(pfile, 0, seek_set); //指標移動到檔案開頭

fread(pbuffer, 1, len, pfile); //讀取檔案

fclose(pfile);

//等價於std::string s;s.assign(pbuffer,len);

pbuffer[len] = 0;

std::string strread(pbuffer,len);

delete pbuffer;

return strread;

}std::vector

> ccsvparse::parsecsvfile(const

std::string &filename,const

std::string &separator)

float used = (float)(clock()-before)/clocks_per_sec;

std::cout

<"s"

}void ccsvparse::printparsedata() const

std::cout

<<"\n"

<};

}

解析csv檔案

以下內容僅為記錄 第一步 解析csv檔案 test public void testcsv throws exception catch filenotfoundexception e string line string everyline try system.out.println csv 中...

CSV檔案解析

實現功能 針對讀取的csv文件字串進行map格式輸出 主要 如下 public class csvpaser extends parser public static void main string args system.out.println map 說明 該部分主要針對多條記錄做相應的解析 ...

AT命令簡易解析

at命令的應用場合很多,也是應用最為通用的命令。而at命令並不像通訊協議特定的資料幀格式,解析at命令需要匹配每個字元。at命令一般有三種狀態 查詢 執行 設定,根據這三種狀態,本文將使用一種簡易的方法解析at命令。巨集定義at命令的三種狀態 define query cmd 0x01 查詢命令 d...