一个操作cvs格式的c++类
经常需要使用excel,或者把有的数据用excel打开,程序可以生成cvs格式的文件,这样就可以excel打开并处理了,于是找了一个处理cvs的c++类跟大家分享
代码出处找不到了:
代码如下:
StringParser.h
#pragma once
#include <process.h>
#include <Windows.h>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <list> typedef char i8;
typedef unsigned char u8;
typedef short i16;
typedef unsigned short u16;
typedef long int i32;
typedef unsigned long u32; namespace StringParser{ //从分隔符中获得数据
inline int GetParamFromString(std::string Str, std::vector<i32>& IntVec, char Delim = ',')
{
char* p = strtok((char*)Str.c_str(), &Delim);
while (p)
{
IntVec.push_back(atoi(p));
p = strtok(NULL, &Delim);
}
return IntVec.size();
} inline int GetParamFromString(std::string Str, std::vector<float>& FloatVec, char Delim = ',')
{
char* p = strtok((char*)Str.c_str(), &Delim);
while (p)
{
FloatVec.push_back(atof(p));
p = strtok(NULL, &Delim);
}
return FloatVec.size();
} inline int GetParamFromString(std::string Str, std::vector<u32>& uiIntVec, char Delim = ',')
{
char* p = strtok((char*)Str.c_str(), &Delim);
while (p)
{
uiIntVec.push_back(strtoul(p, NULL, 10));
p = strtok(NULL, &Delim);
}
return uiIntVec.size();
} inline int GetParamFromString(std::string Str, std::vector<std::string>& StringVec, char Delim = ',')
{
char* p = strtok((char*)Str.c_str(), &Delim);
while (p)
{
std::string buffer = p;
StringVec.push_back(buffer);
p = strtok(NULL, &Delim);
}
return StringVec.size();
} //以左右符号得到括号中的数据ex:[3.1415;0.125][1000;9999]
template<typename T>
int GetParamFromArea(std::string Str, std::vector<std::vector<T> >& IntVec, char left = '[', char right = ']', char Delim = ';')
{
char* pTarget = (char*)Str.c_str();
for (;;)
{
char* pLeft = strchr(pTarget, left);
char* pRight = strchr(pTarget, right);
if (pLeft && pRight)
{
std::string strbuff;
strbuff.insert(0, ++pLeft, pRight-pLeft); std::vector<T> Intbuff;
if (GetParamFromString(strbuff, Intbuff, Delim))
{
IntVec.push_back(Intbuff);
}
pTarget = ++pRight;
}
else
{
break;
}
}
return IntVec.size();
} };
CCSVOperator.h
#pragma once
#include "StringParser.h" class CCSVOperator
{ public:
CCSVOperator(){};
~CCSVOperator(){};
CCSVOperator(const char* path); bool LoadCSV(const char* path);
bool SaveCSV(const char* path = NULL); bool GetInt(u32 uiLine, u32 uiRow, int& iValue);
bool GetFloat(u32 uiLine, u32 uiRow, float& fValue);
std::string* GetString(u32 uiLine, u32 uiRow);
bool SetNumber(u32 uiLine, u32 uiRow, int iValue);
bool SetNumber(u32 uiLine, u32 uiRow, float fValue);
bool SetString(u32 uiLine, u32 uiRow, const char* pStr);
std::map<u32, std::map<u32, std::string> >& GetCSVMap(){return m_StringKeyMap;} protected:
std::string m_CSVName;
std::map<u32, std::map<u32, std::string> > m_StringKeyMap;
public:
int indexOfLines; //行数
int indexOfColumn; //列数,有可能出现列长不一样的情况 };
CSVOperator.cpp
#include "CSVOperator.h" //////////////////////////////////////////////////////////////////////////
//CSV operator CCSVOperator::CCSVOperator(const char* path)
{
LoadCSV(path);
} bool CCSVOperator::LoadCSV(const char* path)
{
indexOfLines = 0;
indexOfColumn = 0;
FILE* pfile = fopen(path, "r");
if (pfile)
{
fseek(pfile,0,SEEK_END);
u32 dwsize = ftell(pfile);
rewind(pfile);// 指针回到文件开头 char* filebuffer = new char[dwsize];
fread(filebuffer, 1, dwsize, pfile); std::map<u32, std::string> StringMap;
char* pBegin = filebuffer;
char* pEnd = strchr(filebuffer, '\n');//查找换行首次出现的位置
u32 uiIndex = 1;
while (pEnd != NULL)
{
std::string strbuff;
strbuff.insert(0, pBegin, pEnd-pBegin);
if (!strbuff.empty())
{
StringMap[uiIndex] = strbuff;
}
pBegin = pEnd + 1;
pEnd = strchr(pEnd + 1, '\n');
++uiIndex;
} indexOfLines = uiIndex - 1; delete[] filebuffer; std::map<u32, std::string>::iterator iter = StringMap.begin();
for (; iter != StringMap.end(); ++iter)
{
std::vector<std::string> StringVec;
std::map<u32, std::string> l_StringMap;
StringParser::GetParamFromString(iter->second, StringVec); if (indexOfColumn< StringVec.size())
{
indexOfColumn = StringVec.size();//保存最大的列数
} for (int i = 0; i < StringVec.size(); ++i)
{
l_StringMap[i+1] = StringVec.at(i);
} m_StringKeyMap[iter->first] = l_StringMap;
}
fclose(pfile);
m_CSVName = path;
return true;
} return false;
} bool CCSVOperator::GetInt(u32 uiLine, u32 uiRow, int& iValue)
{
std::string* pKey = GetString(uiLine, uiRow);
if (pKey)
{
iValue = atoi(pKey->c_str());
return true;
}
else
{
return false;
}
} bool CCSVOperator::GetFloat(u32 uiLine, u32 uiRow, float& fValue)
{
std::string* pKey = GetString(uiLine, uiRow);
if (pKey)
{
fValue = atof(pKey->c_str());
return true;
}
else
{
return false;
}
} std::string* CCSVOperator::GetString(u32 uiLine, u32 uiRow)
{
std::map<u32, std::map<u32, std::string> >::iterator iterLine = m_StringKeyMap.find(uiLine);
if (iterLine != m_StringKeyMap.end())
{
std::map<u32, std::string>& rStringMap = iterLine->second;
std::map<u32, std::string>::iterator iterRow = rStringMap.find(uiRow);
if (iterRow != rStringMap.end())
{
return &iterRow->second;
}
else
{
return NULL;
}
}
else
{
return NULL;
}
} bool CCSVOperator::SetNumber(u32 uiLine, u32 uiRow, int iValue)
{
std::string* pKey = GetString(uiLine, uiRow);
if (pKey)
{
char buffer[100];
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "%d", iValue);
pKey->clear();
*pKey = buffer;
return true;
}
else
{
return false;
}
} bool CCSVOperator::SetNumber(u32 uiLine, u32 uiRow, float fValue)
{
std::string* pKey = GetString(uiLine, uiRow);
if (pKey)
{
char buffer[100];
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "%d", fValue);
pKey->clear();
*pKey = buffer;
return true;
}
else
{
return false;
}
} bool CCSVOperator::SetString(u32 uiLine, u32 uiRow, const char* pStr)
{
std::string* pKey = GetString(uiLine, uiRow);
if (pKey)
{
pKey->clear();
*pKey = pStr;
return true;
}
else
{
return false;
}
} bool CCSVOperator::SaveCSV(const char* path)
{
if (path != NULL)
{
m_CSVName = path;
} FILE* pfile = fopen(m_CSVName.c_str(), "w");
if (pfile)
{
std::map<u32, std::map<u32, std::string> >::iterator iter = m_StringKeyMap.begin();
for (; iter != m_StringKeyMap.end(); ++iter)
{
std::map<u32, std::string>& rStringMap = iter->second;
std::map<u32, std::string>::iterator it = rStringMap.begin();
for (; it != rStringMap.end(); ++it)
{
std::string key = it->second;
key += ',';
fwrite(key.c_str(), 1, key.size(), pfile);
}
char Delim = '\n';
fwrite(&Delim, 1, 1, pfile);
}
fclose(pfile);
}
else
{
return false;
} return true;
}
CVS_OP.CPP
// CSV_OP.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "CSVOperator.h"
#include <iostream>
using namespace std; int _tmain(int argc, _TCHAR* argv[])
{ CCSVOperator CSVOperator;
CSVOperator.LoadCSV("画图数据.csv"); cout<<"line: "<<CSVOperator.indexOfLines<<endl;
cout<<"column: "<<CSVOperator.indexOfColumn<<endl; std::string* pString = CSVOperator.GetString(1,600);
if (pString)
{
std::cout<< pString->c_str() << '\n';
} pString = CSVOperator.GetString(2,4);
if (pString)
{
std::cout<< pString->c_str() << '\n';
} //std::string* pString = NULL;
int j = 0;
for (int i = 0,nColConut = CSVOperator.indexOfColumn;i < nColConut ; ++i)
{
if(pString = CSVOperator.GetString(1,i+1))
{
//m_listctrl.InsertColumn(j ,pString->c_str(), LVCFMT_CENTER, 50); // 添加第1列,
//cout<<"\t"<<&pString;
cout<<"\t";
printf(pString->c_str());
++j;
}
} // int _int = 0;
// if (CSVOperator.GetInt(3,1,_int))
// {
// std::cout<< _int <<'\n';
// }
//
float _float = 0.0f;
if (CSVOperator.GetFloat(4,1, _float))
{
std::cout<< _float<<'\n';
} system("pause");
return 0;
}
效果如下:
一个操作cvs格式的c++类的更多相关文章
- C#操作Xml树的扩展类
本文提供一个操作Xml树的扩展类,与将xml字符串直接映射成实体对象的使用方法,供大家参考,学习. 下面附上源码 using System; using System.Collections.Gene ...
- 用CIL写程序:定义一个叫“慕容小匹夫”的类
前文回顾: <用CIL写程序:你好,沃尔德> <用CIL写程序:写个函数做加法> 前言: 今天是乙未羊年的第一天,小匹夫先在这里给各位看官拜个年了.不知道各位看官是否和匹夫一样 ...
- C# DbHelperSQL,操作不同的数据库帮助类 (转载)
本类主要是用来访问Sql数据库而编写的主要功能如下 .数据访问基础类(基于SQ),主要是用来访问SQ数据库的. .得到最大值:是否存在:是否存在(基于SQParameter): . 执行SQL语句,返 ...
- C# DbHelperSQLP,操作不同的数据库帮助类 (转载)
本类主要是用来访问不同数据库而编写的主要功能如下 .数据访问基础类(基于不同数据库),主要是用来访问不同数据库的. .得到最大值:是否存在:是否存在: . 执行SQL和Orace语句,返回影响的记录数 ...
- 用 python 来操作 docx, xlsx 格式文件(二)(使用 docx 库操作 docx 格式文件
docx 库 文章结构: 一.docx 基本用,创建 docx 文件并添加数据 二.深入理解文本格式(format),并设置所格式属性(attribute) 三.深入理解样式(styles),以及如何 ...
- C#字符串数组排序 C#排序算法大全 C#字符串比较方法 一个.NET通用JSON解析/构建类的实现(c#) C#处理Json文件 asp.net使用Jquery+iframe传值问题
C#字符串数组排序 //排序只带字符的数组,不带数字的 private string[] aa ={ "a ", "c ", "b & ...
- 一个比CBitmap更优秀的类 -- CImage类
Visual C++的CBitmap类的功能是比较弱的,它只能显示出在资源中的图标.位图.光标以及图元文件的内容,而不像VB中的Image控件可以显示出绝大多数的外部图像文件(BMP.GIF.JPEG ...
- java自学第4期——:Scanner类、匿名对象介绍、Random类、ArrayList集合、标准类格式、String类、static静态、Arrays工具类、Math类(1)
一.Scanner类 1.api简介: 应用程序编程接口 2.Scanner类: 作用:获取键盘输入的数据 位置: java.util.Scanner. 使用:使用成员方法nextInt() 和 ne ...
- Java操作文件夹的工具类
Java操作文件夹的工具类 import java.io.File; public class DeleteDirectory { /** * 删除单个文件 * @param fileName 要删除 ...
随机推荐
- 用Python最原始的函数模拟eval函数的浮点数运算功能(2)
这应该是我编程以来完成的难度最大的一个函数了.因为可能存在的情况非常多,需要设计合理的参数来控制解析流程.经验概要: 1.大胆假设一些子功能能够实现,看能否建立整个框架.如果在假设的基础上都无法建立, ...
- HTTP状态码总结
HTTP状态码(HTTP Status Code)是用以表示网页服务器HTTP响应状态的3位数字代码.有些 App 端接口与 HTML 接口用的是同一个,所以做移动端开发也有必要了解一下HTTP状态码 ...
- LibVLC自定义插件目录,获取FPS方法
一.自定义插件目录 在Windows平台,使用LibVLC,只需要在VLC官网的nightly builds下载最新的win32 debug或win64 debug包, 解压缩之后,会有libvlc. ...
- linuxsvn源代码版本库建立
linuxsvn源代码版本库建立 下面就要建立代码的版本库做描述: 1. 安装svn版本服务器端 yum install subversion 从镜像下载安装svn服务器端,我们服务器已经安装 ...
- 集成JPA+springmvc+spring+EJB中的Java EE应用
EJB是sun的JavaEE服务器端组件模型,设计目标与核心应用是部署分布式应用程序.凭借java跨平台的优势,用EJB技术部署的分布式系统可以不限于特定的平台.EJB (Enterprise Jav ...
- 【OpenGL】详解第一个OpenGL程序
写在前面 OpenGL能做的事情太多了!很多程序也看起来很复杂.很多人感觉OpenGL晦涩难懂,原因大多是被OpenGL里面各种语句搞得头大,一会gen一下,一会bind一下,一会又active一下. ...
- Cocos2D iOS之旅:如何写一个敲地鼠游戏(二):Cocos2D中的高清支持
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 最简单的基于FFmpeg的封装格式处理:视音频分离器简化版(demuxer-simple)
===================================================== 最简单的基于FFmpeg的封装格式处理系列文章列表: 最简单的基于FFmpeg的封装格式处理 ...
- Linux 进程调度小结
概述 这个问题又是面试常问问题,当时听到感觉太宽泛了,有点大,心里知道但是说不全,这里做一下总结 [1]进程调度的作用 [2]调度德策略 1. 进程调度的作用 ,进程调度就是对进程进行调度,即负责选择 ...
- 从JDK源码角度看java并发线程的中断
线程的定义给我们提供了并发执行多个任务的方式,大多数情况下我们会让每个任务都自行执行结束,这样能保证事务的一致性,但是有时我们希望在任务执行中取消任务,使线程停止.在java中要让线程安全.快速.可靠 ...