csv文件的读取类

头文件 XCFileStream.h
#pragma once
#include <string>
#include <list>
#include <vector>
using namespace std;
#include <windows.h>
/// 其它库头文件
/// 本项目头文件


/// 无错
#define XC_ERR_NONE 0
/// 无效文件名或非指定文件格式
#define XC_ERR_INVALID_FILE_NAME (-1)

class CXCFileStream 
{
	public:


		CXCFileStream(void);
		~CXCFileStream(void);

		/*
		* 函数功能:读取CSV文件,分析其中的内容,然后存储在容器中。
		* 参数描述:
		*     [in] lpszFilename - 待读取的CSV文件;
		*     [in, out] vlStr - 存储分析后的CSV内容
		* 返回值:
		*     错误代码
		* 注意:这里因为特殊愿意(目前还不清楚),不是vector<list<string>>,而是vector<list<string> >。
		*/
		const int ReadCsvData(LPCTSTR lpszFilename, vector<list<string> > &vlStr);
		/* 
		* 函数功能:将容器中的内容经过排版、布局、添加分隔符后写入到CSV文件。
		* 参数描述:
		*     [in] lpszFilename - 待读取的CSV文件;
		*     [in] vlStr - 存储分析后的CSV内容
		* 返回值:
		*     错误代码
		* 注意:这里因为特殊愿意(目前还不清楚),不是vector<list<string>>,而是vector<list<string> >。
		*/
		const int WriteCsvData(LPCTSTR lpszFilename, const vector<list<string> > &vlStr);


	private:
		/// 判断是否是CSV文件
		const bool IsCsvFile(LPCTSTR lpszFilename);


};

XCFileStream.cpp文件
</pre><pre name="code" class="cpp">#include "stdafx.h"
#include <fstream> 
#include <algorithm> 

#include "XCFileStream.h"


CXCFileStream::CXCFileStream()
{
}


CXCFileStream::~CXCFileStream(void)
{
}


const bool CXCFileStream::IsCsvFile(LPCTSTR lpszFilename)
{
	/// 0、判断文件名是否有效
	if (NULL == lpszFilename || 4 > strlen(lpszFilename)) 
		return false;
	/// 本地变量
	string _strFileName(lpszFilename);
	size_t _iLen = _strFileName.length();
	string _strSuff(_strFileName.substr(_iLen - 4, 4));
	/// 转变为小写,如果要转变为大写:tolower -> toupper 。
	transform(_strSuff.begin(), _strSuff.end(), _strSuff.begin(), tolower);
	/// 1、判断是否是CSV文件
	return (0 == _strSuff.compare(".csv"));
}


const int CXCFileStream::ReadCsvData(LPCTSTR lpszFilename, vector<list<string>> &vlStr)
{
	/// 1、判断是否是CSV文件
	if (! IsCsvFile(lpszFilename)) 
		return XC_ERR_INVALID_FILE_NAME;
	/// 2、打开CSV文件
	ifstream _streamFromFile(lpszFilename);
	///    判断打开文件是否成功
	if (NULL == _streamFromFile) 
		return (-errno);
	/// 存储读取的文件内容
	string _strIn("");
	/// 3、读取一行
	while (getline(_streamFromFile, _strIn)) {
		/// 每行的源字符串
		LPCTSTR _pcSrc = _strIn.c_str();
		/// 存储一行‘,'分隔解析后的各个元素
		list<string> _ltStr;
		/// Parse values in this line
		while (*_pcSrc != '\0') {
			/// string to hold this value
			string _strElem(""); 
			/// 针对每个字符分析
			if (*_pcSrc == '"') {
				/// Bump past opening quote
				_pcSrc++;
				/// Parse quoted value
				while (*_pcSrc != '\0') {
					/// Test for quote character
					if (*_pcSrc == '"') {
						/// Found one quote
						_pcSrc++;
						// If pair of quotes, keep one
						// Else interpret as end of value
						if (*_pcSrc != '"') {
							_pcSrc++;
							break;
						}
					}
					/// Add this character to value
					_strElem.push_back(*_pcSrc++);
				}
			}
			else {
				// Parse unquoted value
				while (*_pcSrc != '\0' && *_pcSrc != ',') 
					_strElem.push_back(*_pcSrc++);
				// Advance to next character (if not already end of string)
				if (*_pcSrc != '\0')
					_pcSrc++;
			}
			/// Add this string to container
			_ltStr.push_back(_strElem);
		}
		/// 分析后的一行文件内容所得的元素列表添加到容器中
		vlStr.push_back(_ltStr);
		/// 归零,防止下次分析旧的数据。
		_strIn.assign("");
	}
	_streamFromFile.close();
	return XC_ERR_NONE;
}


const int CXCFileStream::WriteCsvData(LPCTSTR lpszFilename, const vector<list<string> > &vlStr)
{
	/// 1、判断是否是CSV文件
	if (! IsCsvFile(lpszFilename)) 
		return XC_ERR_INVALID_FILE_NAME;
	/// 2、打开CSV文件
	ofstream _streamToFile(lpszFilename);
	///    判断打开文件是否成功
	if (NULL == _streamToFile) 
		return (-errno);
	/// 本地变量
	static TCHAR chQuote = '"';
	static TCHAR chComma = ',';
	/// Loop through each list of string in vector 
	for (vector<list<string> >::const_iterator vIt = vlStr.begin(); vIt != vlStr.end(); vIt ++) {
		/// Loop through each string in list 
		for (list<string>::const_iterator lIt = vIt->begin(); lIt != vIt->end(); lIt ++) {
			/// Separate this value from previous
			if (vIt->begin() != lIt) 
				_streamToFile.put(chComma);
			/// 考虑string中可能有,或"的情况,这就要特殊包装。
			bool bComma = (lIt->find(chComma) != lIt->npos);
			bool bQuote = (lIt->find(chQuote) != lIt->npos);
			/// 真的含有,或"的情况
			if (bComma || bQuote) {
				_streamToFile.put(chQuote);
				if (bQuote) {
					for (string::const_iterator chIt = lIt->begin(); chIt != lIt->end(); chIt ++ ) {
						// Pairs of quotes interpreted as single quote
						if (chQuote == *chIt) 
							_streamToFile.put(chQuote);
						_streamToFile.put(*chIt);
					}
				}
				else 
					_streamToFile << *lIt;

				_streamToFile.put(chQuote);
			}
			else
				_streamToFile << *lIt;
		}
		/// 换行
		_streamToFile << endl;
	}
	/// 
	_streamToFile.close();
	return XC_ERR_NONE;
}

操作示例代码

CXCFileStream file;
vector<list<string> > it;
list<string> lt,lt1;
lt.push_back("123");
lt.push_back("345");


lt1.push_back("111");
lt1.push_back("222");


it.push_back(lt);
it.push_back(lt1);


file.WriteCsvData("b.csv",it);

vector<list<string>> IT;
file.ReadCsvData("b.csv",IT);
vector<list<string>>::const_iterator vit_c;
list<string>::const_iterator lit_c;
for(vit_c = IT.begin();vit_c != IT.end();vit_c++)
{
for(lit_c = vit_c->begin();lit_c != vit_c->end();lit_c++)
AfxMessageBox(lit_c->c_str());
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蚂蚁_CrkRes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值