视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
csv文件解析
2025-10-05 01:24:01 责编:小OO
文档
/**

3. * 夝愅姰揑悢悩曻摓data?槩擇?悢?拞丆捠?getData(int rows,int cols)

4. * ?庢悢悩

5. **/

6.

7.#ifndef _CSVPARSE_

8.#define _CSVPARSE_

9.

10.#include "cocos2d.h"

11.#include

12.#include

13.#include

14.

15.class CSVParse {

16.

17.public:

18. CSVParse(istream& fin = cin, string sep =

g fld;

18. int i, j;

19.

20. if (line.length() == 0)

21. return ;

22. i = 0;

23.

24. do {

25. if (i < line.length() && line[i] == '"')

26. j = advquoted(line, fld, ++i); // skip quote

27. else

28. j = advplain(line, fld, i);

29.

30. field.push_back(fld);

31. i = j + 1;

32. } while (j < line.length());

33.

34.}

35.

36.// advquoted: quoted field; return index of next separator

37.int CSVParse::advquoted(const string& s, string& fld, int i)

38.{

39. int j;

40.

41. fld = "";

42. for (j = i; j < s.length(); j++)

43. {

44. if (s[j] == '"' && s[++j] != '"')

45. {

46. int k = s.find_first_of(fieldsep, j);

47. if (k > s.length()) // no separator found

48. k = s.length();

49. for (k -= j; k-- > 0; )

50. fld += s[j++];

51. break;

52. }

53. fld += s[j];

54. }

55. return j;

56.}

57.

58.// advplain: unquoted field; return index of next separator

59.int CSVParse::advplain(const string& s, string& fld, int i)

60.{

61. int j;

62.

63. j = s.find_first_of(fieldsep, i); // look for separator

. if (j > s.length()) // none found

65. j = s.length();

66. fld = string(s, i, j-i);

67. return j;

68.}

69.

70.

71.// getfield: return n-th field

72.const char* CSVParse::getData(int rows,int cols)

73.{

74. if (rows<0||rows>=data.size()||cols<0||cols>=data[rows].size()) {

75. return "";

76. }

77.

78. return data[rows][cols].c_str();

79.}

80.

81.int CSVParse::findColsData(int cols,const char* value)

82.{

83. for (int i=0; i84. if (strcmp(getData(i, cols), value)==0 )

85. {

86. return i;

87. }

88. }

. return -1;

90.}

91.//梡槹寭梕windows壓揑?峴晞\

\

92.//尨場惀嵼windows壓?峴晞?\

\

丆帶嵼unix壓\

93.//?峴晻憰,斲庢揑?峴?庢揑嵟岪堦槩悢悩嘁?夛桳槩?奜揑\

94.bool getline(std::ifstream &is,std::string &str){

95. bool b = std::getline(is,str);

96. std::string::size_type p = str.find_last_of('\

');

97. if(p != std::string::npos) str.erase(p);

98. return b;

99.}

100.

101.//?庢曽幃: 拃峴?庢, 彨峴?擖帤晞孁, 峴擵?梡夞峴嬫暘

102.//If you want to avoid reading into character arrays,

103.//you can use the C++ string getline() function to read lines into strings

104.bool CSVParse::openFile(const char* fileName)

105.{

106. string pathKey = CCFileUtils::fullPathFromRelativePath(fileName);

107. ifstream fin(pathKey.c_str());

108.

string s;

109.

110. while(::getline(fin,s))

111. {

112. CCLOG("%s下载本文

显示全文
专题