1. 程式人生 > >csv檔案的讀取類

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());
}