C++ 读入csv 文件程序

该类其实是大部分模仿 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 file
		ifstream 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 vector
		map<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)
{
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值