CSV(逗号分隔值文件格式)

逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。通常,所有记录都有完全相同的字段序列。

CSV文件格式的通用标准并不存在,但是在RFC 4180中有基础性的描述。使用的字符编码同样没有被指定,但是7-bitASCII是最基本的通用编码。

这种文件格式经常用来作为不同程序之间的数据交互的格式。

具体文件格式:每条记录占一行 以逗号为分隔符 逗号前后的空格会被忽略 字段中包含有逗号,该字段必须用双引号括起来 字段中包含有换行符,该字段必须用双引号括起来 字段前后包含有空格,该字段必须用双引号括起来 字段中的双引号用两个双引号表示 字段中如果有双引号,该字段必须用双引号括起来 第一条记录,可以是字段名

John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123

#include <iostream>
#include <fstream> ifstream file(m_strFilePath);
std::string row;
vector<string> infRow;
getline(file, row);//读取第一行
MySplit(row, infRow); //获取每列的内容 while (file.good())
{
//读取每一行
getline(file, row);
CStringA strRow = row.c_str();
strRow.Replace("\\", "\\\\");
strRow.Replace("'", "\\'");
row = strRow;
infRow.clear();
MySplit(row, infRow);
//...
} char *m_pcTemp;
int m_iMaxTempLength; m_pcTemp(NULL), m_iMaxTempLength() //csv
void PushInTemp(const char *pcCursor, int iLen)
{
if (iLen >= m_iMaxTempLength)
{
m_iMaxTempLength = iLen * ;
if (m_pcTemp)
{
delete[] m_pcTemp;
m_pcTemp = nullptr;
} m_pcTemp = new char[m_iMaxTempLength];
}
if (iLen > )
{
if (!m_pcTemp)
{
m_pcTemp = new char[m_iMaxTempLength];
} memcpy(m_pcTemp, pcCursor, iLen);
} if(m_pcTemp)
m_pcTemp[iLen] = '\0';
} //csv
void MySplit(std::string &row, vector<string> &infRow)
{
const char *pcCursor = row.c_str();
const char *pcComma = NULL;
const char *pcQuot = NULL; do
{
if ( == row.size())
{
break;
}
pcComma = strchr(pcCursor, ',');
pcQuot = strchr(pcCursor, '"'); if (NULL == pcComma && NULL == pcQuot)
{
infRow.push_back(pcCursor);
break;
}
if (NULL == pcQuot)
{
int iLen = (int)pcComma - (int)pcCursor; PushInTemp(pcCursor, iLen);
infRow.push_back(m_pcTemp);
pcCursor += iLen + ;
}
else if (NULL == pcComma)
{
const char *pcLastQuot = strrchr(pcCursor, '"');
int iLen = (int)pcLastQuot - (int)pcQuot - ; PushInTemp(pcQuot + , iLen);
infRow.push_back(m_pcTemp);
break;
}
else
{
int iCommaPos = (int)pcComma;
int iQuotPos = (int)pcQuot; if (iCommaPos < iQuotPos)
{
int iLen = (int)pcComma - (int)pcCursor;
PushInTemp(pcCursor, iLen);
infRow.push_back(m_pcTemp);
pcCursor += iLen + ;
}
else
{
const char *pcNextQuot = NULL;
int iMove = (int)pcQuot - (int)pcCursor + ;
std::string strQuotData; pcCursor += iMove; do
{
pcNextQuot = strchr(pcCursor, '"');
if (NULL == pcNextQuot)
{
goto end;
}
if (*(pcNextQuot + ) == '"')
{
int iLen = (int)pcNextQuot - (int)pcCursor;
PushInTemp(pcCursor, iLen);
strQuotData += m_pcTemp;
pcCursor = pcNextQuot + ;
strQuotData += '"';
}
else
{
int iLen = (int)pcNextQuot - (int)pcCursor;
PushInTemp(pcCursor, iLen);
strQuotData += m_pcTemp;
infRow.push_back(strQuotData);
pcCursor += iLen + ;
if (*pcCursor == ',')
{
++pcCursor;
}
break;
}
} while ();
}
}
if (*pcCursor == '\0')
{
break;
}
} while (); end:
return;
}

最新文章

  1. 微信公众账号第三方平台全网发布源码(java)- 实战测试通过
  2. 一个java页游服务器框架
  3. 运用datalist标签实现用户的搜索列表
  4. Java获取前天和后天的时间
  5. DHCP和NAT的概念与对比
  6. Pro ASP.NET MVC 5 Framework.学习笔记.6.4.MVC的必备工具
  7. 新蒂下午茶体基本版SentyTEA-Basic
  8. 2016 Al-Baath University Training Camp Contest-1 C
  9. json与jsonp应用及其他ajax数据交互方式
  10. [Hive - LanguageManual ] Windowing and Analytics Functions (待)
  11. Redis源码阅读笔记(1)——简单动态字符串sds实现原理
  12. 在本地调试微信项目(C#)
  13. mybatis配置文件xxxx.xml中缺失返回类型的后果A query was run and no Result Maps were found
  14. input 等替换元素的baseline问题
  15. Quartz.NET学习笔记(一) 简介
  16. javascript_01
  17. Linux大学实验
  18. 事务理解及Spring中的事务
  19. 线程&amp;进程
  20. Golang切片的三种简单使用方式及区别

热门文章

  1. CentOS6.5使用yum快速搭建LAMP环境
  2. 【C#】侦听文件系统更改通知 FileSystemWatcher 类
  3. scala函数式编程(一)
  4. 基于Arcface 免费离线人脸识别 2.0 Demo C#
  5. Spark Streaming笔记
  6. pymysql 数据库编程
  7. (转)c# String与StringBuilder
  8. boke练习: spring boot: security post数据时,要么关闭crst,要么添加隐藏域
  9. eclipse----&gt;error and exception 和常用配置
  10. springboot----&gt;javax.servlet.ServletException