C++ 读入csv 文件程序
来源:互联网 发布:淘宝网用户名大全 编辑:程序博客网 时间:2024/05/21 16:21
该类其实是大部分模仿 http://blog.csdn.net/zlhy_/article/details/8764250其中有些许改变;大概思路是: 文件流读入所有内容 根据'\n'获取每行的内容 根据','获取每个单元格内容 将结果存储在(i,j)标记的map 中读入其他文件也可以按照该思路进行#pragma once //#include "stringparser.h" #include <assert.h> #include <map> #include <vector> #include <string> #include<fstream>#include<iostream>#include<iomanip>#include <string>#include <cstring>#include <sstream>//#include "SkillRecord.h" using namespace std; typedef unsigned long u32; class CReadPFfromCSV{private: map<u32, map<u32, string>> m_stringMap; string m_CSVName; public:CReadPFfromCSV(void);CReadPFfromCSV(const char *path) { assert(LoadCSV(path)); } bool LoadCSV(const char *path); bool SaveCSV(const char *path = NULL); bool GetIntValue(u32 uiRow, u32 uiCol, int &riValue); bool GetFloatValue(u32 uiRow, u32 uiCol, float &rfValue); string* GetStringValue(u32 uiRow, u32 uiCol); int GetParamFromString(string str, vector<string> &stringVec, char delim = ','); map<u32, map<u32, string>>& GetCSVMap() { return m_stringMap; } ~CReadPFfromCSV(void);};
#include "stdafx.h"#include "ReadPFfromCSV.h"bool CReadPFfromCSV::LoadCSV(const char *path) { FILE *pFile = fopen(path, "r"); if (pFile) { //read stream from fileifstream in(path, ios::in);istreambuf_iterator<char> beg(in), end;string strdata(beg, end);in.close();//convert from string to char[]const char*cfirst = strdata.c_str();char*fileBuffer = new char[strlen(cfirst) + 1];strcpy( fileBuffer, cfirst);map<u32, string> stringMap; u32 uiIndex = 1; char *pBegin = fileBuffer; char *pEnd = strchr(pBegin, '\n'); pBegin = pEnd + 1; pEnd = strchr(pBegin, '\n');pBegin = pEnd + 1; pEnd = strchr(pBegin, '\n'); /*int icount = 0;while(icount<1132){ pBegin = pEnd + 1; pEnd = strchr(pBegin, '\n'); icount++;}*/while (pEnd) { string strTemp; strTemp.insert(0, pBegin, pEnd-pBegin); assert(!strTemp.empty()); stringMap[uiIndex++] = strTemp; pBegin = pEnd + 1; pEnd = strchr(pBegin, '\n'); //cout<<strTemp<<endl;//cout<<stringMap.size()<<endl;} delete []fileBuffer; fileBuffer = NULL; pBegin = NULL; pEnd = NULL; //cout<<stringMap.size()<<endl;//read every element in each line to map:stringMapTemp and to vectormap<u32, string>::iterator iter = stringMap.begin(); for (; iter != stringMap.end(); ++iter) { vector<string> stringVec; map<u32, string> stringMapTemp; assert(GetParamFromString(iter->second, stringVec) > 0); vector<string>::size_type idx = 0; for (; idx != stringVec.size(); ++idx) { stringMapTemp[idx + 1] = stringVec[idx]; } m_stringMap[iter->first] = stringMapTemp; } fclose(pFile); m_CSVName = path; return true; } else { return false; } } bool CReadPFfromCSV::SaveCSV(const char *path /* = NULL */) { if (path != NULL) { m_CSVName = path; } FILE *pFile = fopen(m_CSVName.c_str(), "w"); if (pFile) { map<u32, map<u32, string>>::iterator iter = m_stringMap.begin(); for (; iter != m_stringMap.end(); ++iter) { map<u32, string> &rStringMap = iter->second; map<u32, string>::iterator it = rStringMap.begin(); for (; it != rStringMap.end(); ++it) { string strTemp = it->second; strTemp += ','; fwrite(strTemp.c_str(), 1, 1, pFile); } char delim = '\n'; fwrite(&delim, 1, 1, pFile); } fclose(pFile); return true; } else { return false; } } bool CReadPFfromCSV::GetIntValue(u32 uiRow, u32 uiCol, int &riValue) { string *pStr = GetStringValue(uiRow, uiCol); if (pStr) { riValue = atoi(pStr->c_str()); return true; } else { return false; } } bool CReadPFfromCSV::GetFloatValue(u32 uiRow, u32 uiCol, float &rfValue) { string *pStr = GetStringValue(uiRow, uiCol); if (pStr) { rfValue = atof(pStr->c_str()); return true; } else { return false; } } string* CReadPFfromCSV::GetStringValue(u32 uiRow, u32 uiCol) { map<u32, map<u32, string>>::iterator iter = m_stringMap.find(uiRow); if (iter != m_stringMap.end()) { map<u32, string> &rStrMap = iter->second; map<u32, string>::iterator it = rStrMap.find(uiCol); if (it != rStrMap.end()) { return &(it->second); } else { return NULL; } } else { return NULL; } } //用于分割字符串,将CSV表格中的一行按照规则解析成一组字符串,存储在一个vector中 //根据CSV表格中所存储的数据的不同,重载各函数 int CReadPFfromCSV::GetParamFromString(string str, vector<string> &stringVec, char delim) { char *token = strtok(const_cast<char *>(str.c_str()), &delim); while (token) { string strTemp = token; stringVec.push_back(strTemp); token = strtok(NULL, &delim); //cout<<strTemp<<endl;} return stringVec.size(); } /*void CReadPFfromCSV::GetSkillRecordMapTable(map<int, SkillRecord> &sSkillMapTable) { map<u32, map<u32, string>>::iterator iter = m_stringMap.begin(); for (; iter != m_stringMap.end(); ++iter) { map<u32, string> strmap = iter->second; SkillRecord skillTemp; skillTemp.SetID(atoi(strmap[1].c_str())); skillTemp.SetPath(strmap[2]); skillTemp.SetName(strmap[3]); skillTemp.SetHurt(atoi(strmap[4].c_str())); skillTemp.SetPlayTime(atoi(strmap[5].c_str())); sSkillMapTable[skillTemp.GetID()] = skillTemp; } } */CReadPFfromCSV::CReadPFfromCSV(void){}CReadPFfromCSV::~CReadPFfromCSV(void){}
0 0
- C++ 读入csv 文件程序
- 将csv文件读入到数组里
- 抓取数据为json文件,读入csv
- 【C/C++】C++读入文件
- python小程序:把名称列表从csv文件读入mongo,再从mongo导入redis(anaconda3.5.2)
- 【c++】csv文件读写
- 【C++】读写CSV文件
- 【C#】CSV文件读写
- spring batch批处理框架把csv文件读入数据库
- 练习写C++代码(111)-读入.csv文件
- C++将csv文件数据读入数组中
- c++/c的文件读入和字符串读入总结
- 读取csv文件(C#,C++)
- c/c++ 数据文件读入测试程序
- 如何把文件中的数据读入程序
- C语言解析CSV文件!
- C语言读取CSV文件
- C语言解析csv文件
- 用《内网穿山甲》共享内网中的远程桌面服务
- MySql中的悲观锁和乐观锁
- 1、Power View—使用前的准备
- Android判断网络状态的工具类
- 一个菜鸟实习生五个月的分享
- C++ 读入csv 文件程序
- AndroidStdio导入项目
- 《大型数据库作业1》掌握oracle数据库连接的基本原理和具体操作过程
- Spring mvc jsp中引入静态css资源文件问题
- js面向对象的简单例子
- Up Board介绍及上手体验
- 从源码分析ArrayList和LinkedList的差别
- Google的访问
- 银行系统的功能测试用例