C++读写CSV文件
前两天看了《Reading and Writing CSV Files in MFC》(http://www.codeproject.com/Articles/53759/Reading-and-Writing-CSV-Files-in-MFC)发现它只针对文件中的一行读写,而且只能作为一个局部变量使用,或者读,或者写,不能同时兼用,更不能作为类的变量,更别说扩展了。而且,它只支持在MFC条件下实现,如果我们需要在一个模块当中呢?在一个库中呢?在非MFC中呢?不过,它的读取遍历算法和写入算法我觉得还是不错的,所以,基于此,也根据我自己工作中的需求做了改变,也在测试中达到了比较好的结果。
一般读取CSV文件我们是需要:
1、既能满足MFC,又能满足非MFC;
2、逐行读取,而不是一行,因为可能不止一行;
3、读取一行后需要针对分隔符(一般是“,”)来分析获得所有解析后的元素项,而且每一行的元素个数都不一定相同;
4、写入CSV时,针对每一行的元素之间都要添加分隔符(一般是“,”),甚至元素当中可能有诸如:冒号(“"”)、逗号(“,”)这样比较特殊的字符,我们又如何解决呢?
5、作为一个封装类,留有余地便于今后的完善和扩展。
好了,不多说了,上代码:
XCFileStream.h
#if _MSC_VER > 1000
#pragma once
#endif
#ifndef __XCFILESTREAM_H__
#define __XCFILESTREAM_H__
/// 隐藏依赖文件
/// C系统文件
/// C++系统文件
#include <string>
#include <list>
#include <vector>
using namespace std;
#include <windows.h>
/// 其它库头文件
/// 本项目头文件
/// 无错
#define XC_ERR_NONE 0
/// 无效文件名或非指定文件格式
#define XC_ERR_INVALID_FILE_NAME (-1)
/// xun-test文件的读与写
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);
};
#endif
XCFileStream.cpp
/// 隐藏依赖文件
/// C系统文件
/// C++系统文件
#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("");
}
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;
}
///
return XC_ERR_NONE;
}
如果朋友你有更好的建议或这里有问题,请根据blog抬头我的联系方式跟我取得联系,感谢你的支持和帮助。
C++读写CSV文件的更多相关文章
- 用opencsv文件读写CSV文件
首先明白csv文件长啥样儿: 用excel打开就变成表格了,看不到细节 推荐用其它简单粗暴一点儿的编辑器,比如Notepad++, csv文件内容如下: csv文件默认用逗号分隔各列. 有了基础的了解 ...
- 使用Python读写csv文件的三种方法
Python读写csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 前言 逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是 ...
- python3读写csv文件
python读取CSV文件 python中有一个读写csv文件的包,直接import csv即可.利用这个python包可以很方便对csv文件进行操作,一些简单的用法如下. 1. 读文件 csv_ ...
- python读写csv文件
文章链接:https://www.cnblogs.com/cloud-ken/p/8432999.html Python读写csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 前言 逗 ...
- 利用JavaCSV API来读写csv文件
http://blog.csdn.net/loongshawn/article/details/53423121 http://javacsv.sourceforge.net/ 转载请注明来源-作者@ ...
- 使用 Apache Commons CSV 读写 CSV 文件
有时候,我们需要读写 CSV 文件,在这里给大家分享Apache Commons CSV,读写 CSV 文件非常方便. 具体官方文档请访问Apache Commons CSV. 官方文档已经写得很详细 ...
- python3使用csv包,读写csv文件
python操作csv,现在很多都用pandas包了,不过python还是有一个原始的包可以直接操作csv,或者excel的,下面举个例子说明csv读写csv文件的方法: import os impo ...
- C/C++读写csv文件
博客转载自:http://blog.csdn.net/u012234115/article/details/64465398 C++ 读写CSV文件,注意一下格式即可 #include <ios ...
- JAVA读写CSV文件
最近工作需要,需要读写CSV文件的数据,简单封装了一下 依赖读写CSV文件只需引用`javacsv`这个依赖就可以了 <dependency> <groupId>net.sou ...
- (Python基础教程之十二)Python读写CSV文件
Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...
随机推荐
- mysql分表方法-----MRG_MyISAM引擎分表法
一般来说,当我们的数据库的数据超过了100w记录的时候就应该考虑分表或者分区了,这次我来具体说说分表的一些方法.眼下我所知道的方法都是MYISAM的,INNODB怎样做分表而且保留事务和外键,我还不是 ...
- JavaScript异步编程 ( 一 )
1. 异步编程 Javascript语言的执行环境是"单线程"(single thread).所谓"单线程",就是指一次只能完成一件任务.如果有多个任务,就必须 ...
- mybatis知识总结
基于昨天的mybatis入门详解,今天我们再来看看mybatis稍微高深些的知识点. 1.解决Model属性和数据库字段不一致的问题 1),开启驼峰命名 2),使用resultMap进行映射, < ...
- (转)使用 .NET 的 RNGCryptoServiceProvider 生成随机数
1. [代码]一个简单的方法,但不够可靠 跳至 [1] [2] [全屏预览] ? 1 2 3 4 5 6 7 8 9 10 11 static void Main(string[] args) ...
- channel c3 disabled, job failed on it will be run on another channel
今天执行备份时,报错: allocated channel: c3 channel c3: sid=131 instance=orcl2 devtype=DISK RMAN-03009: fail ...
- 服务器端操作Cookie[2]
服务器端操作Cookie,主要注意会使用以下三个类: HttpCookie,HttpResponse,HttpRequest 关于HttpCookie: 属性 描述 例子 Domain 获取或设置与此 ...
- SDP (Session Description Protocol)
SDP的描述网络上一大堆中文的,可以看看RFC2327 SDP 信息是文本信息,采用 UTF-8 编 码中的 ISO 10646 字符集.SDP 会话描述如下:(标注 * 符号的表示可选字段): v ...
- Caesar cipher
#include <iostream> using namespace std; int main() {int k,i; char s[5]; cin>>k; for(; ...
- uC/OS-II中的中断(转)
中断是指在程序运行过程中,应内部或外部异步事件的请求中止当前任务,而去处理异步事件所要求的任务的过程. 中断服务函数(ISR)是应中断请求而运行的程序. 中断向量就是中断服务函数(ISR)的入口地址, ...
- DataTables语言国际化
$('#example').DataTable({ language: { "sProcessing": "处理中...", ...