csv2txt.cpp
#include <iostream>
#include <fstream.h>
#include <windows.h>
#include <iomanip.h>
#pragma once
#include <process.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;
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);
std::string* GetString(u32 uiLine, u32 uiRow);
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;
};
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, ));
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(, ++pLeft, pRight-pLeft);
std::vector<T> Intbuff;
if (GetParamFromString(strbuff, Intbuff, Delim))
{
IntVec.push_back(Intbuff);
}
pTarget = ++pRight;
}
else
{
break;
}
}
return IntVec.size();
}
};
CCSVOperator::CCSVOperator(const char* path)
{
LoadCSV(path);
}
bool CCSVOperator::LoadCSV(const char* path)
{
FILE* pfile = fopen(path, "r");
if (pfile)
{
fseek(pfile,,SEEK_END);
u32 dwsize = ftell(pfile);
rewind(pfile);
char* filebuffer = new char[dwsize];
fread(filebuffer, , dwsize, pfile);
std::map<u32, std::string> StringMap;
char* pBegin = filebuffer;
char* pEnd = strchr(filebuffer, '\n');
u32 uiIndex = ;
while (pEnd != NULL)
{
std::string strbuff;
strbuff.insert(, pBegin, pEnd-pBegin);
if (!strbuff.empty())
{
StringMap[uiIndex] = strbuff;
}
pBegin = pEnd + ;
pEnd = strchr(pEnd + , '\n');
++uiIndex;
}
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);
; i < StringVec.size(); ++i)
{
l_StringMap[i+] = 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;
}
}
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;
}
}
int main(int argc, char* argv[])
{
int OtdrTestsNum;//total num of OTDR test datas
int OtdrEventsNum;//total num of OTDR test datas
CCSVOperator CSVOperator;
if(!CSVOperator.LoadCSV("file.csv"))
{
printf("没有找到csv文档,请将csv文档放在本目录下!\n");
system("pause");
return false;
}
CreateDirectory("OTDR_Test_Out",NULL);
if( !SetCurrentDirectory("OTDR_Test_Out"))
{
printf("设置输出目录失败!\n");
}
//OTDR Information
ofstream out1("OTDR_Information.txt");
u32 uiRow=,uiColumn=;
out1<<CSVOperator.GetString(,)->c_str()<<,)->c_str()<<'\n';
out1<<CSVOperator.GetString(,)->c_str()<<'\n';
out1<<CSVOperator.GetString(,)->c_str()<<"\n\n";
out1<<CSVOperator.GetString(,)->c_str()<<,)->c_str()<<'\n';
;uiRow<=;uiRow++)
{
;uiColumn<=;uiColumn++)
{
out1<<setiosflags(ios::left)<<setw()<<CSVOperator.GetString(uiRow,uiColumn)->c_str();
}
out1<<'\n';
}
out1<<)->c_str()<<)->c_str()<<'\n';
;uiRow++)
{
;uiColumn<=;uiColumn++)
{
out1<<setiosflags(ios::left)<<setw()<<CSVOperator.GetString(uiRow,uiColumn)->c_str();
}
out1<<'\n';
}
//OTDR Test Datas
ofstream out2("OTDR_Test_Datas.txt");
out2<< CSVOperator.GetString(,)->c_str() << ,)->c_str() ;
CSVOperator.GetInt(,,OtdrTestsNum);//get datas num
out2 << ,)->c_str();
;uiRow<OtdrTestsNum+;uiRow++)
{
out2 << <<)->c_str();
}
//OTDR Events
ofstream out3("OTDR_Events.txt");
out3<< CSVOperator.GetString(+OtdrTestsNum,)->c_str()<<+OtdrTestsNum,)->c_str();
CSVOperator.GetInt(OtdrTestsNum+,,OtdrEventsNum);//get Events num
+OtdrTestsNum+;uiRow<+OtdrTestsNum+OtdrEventsNum+;uiRow++)
{
out3 << '\n';
;uiColumn<=;uiColumn++)
{
std::string* pString = CSVOperator.GetString(uiRow,uiColumn);
if (pString)
{
)!=)
out3<<setiosflags(ios::left)<<setw()<<pString->c_str();
else
out3<<pString->c_str();
}
}
}
;
}
csv2txt.cpp的更多相关文章
- 使用“Cocos引擎”创建的cpp工程如何在VS中调试Cocos2d-x源码
前段时间Cocos2d-x更新了一个Cocos引擎,这是一个集合源码,IDE,Studio这一家老小的整合包,我们可以使用这个Cocos引擎来创建我们的项目. 在Cocos2d-x被整合到Cocos引 ...
- Json CPP 中文支持与入门示例
在每一个Json Cpp自带*.cpp文件头加上: #include "stdafx.h" 将Json Cpp对自带的头文件的引用修改为单引号方式,例如json_reader.cp ...
- cpp 调用python
在用cpp调用python时, 出现致命错误: no module named site , 原因解释器在搜索路径下没有找到python库.可以在调用Py_Initialize前,调用 Py_Se ...
- nginx+fastcgi+c/cpp
参考:http://github.tiankonguse.com/blog/2015/01/19/cgi-nginx-three/ 跟着做了一遍,然后根据记忆写的,不清楚有没错漏步骤,希望多多评论多多 ...
- APM程序分析-ArduCopter.cpp
该文件是APM的主文件. #define SCHED_TASK(func, rate_hz, max_time_micros) SCHED_TASK_CLASS(Copter, &copter ...
- APM程序分析-AC_WPNav.cpp
APM程序分析 主程序在ArduCopter.cpp的loop()函数. /// advance_wp_target_along_track - move target location along ...
- Dev Cpp 输出中文字符问题
最近 c++ 上机作业,vc++6.0 挂了没法用,只好用 Dev Cpp 先顶替一下,然而在遇到输出中文字符的时候出现了乱码的情况,但这种情况又非常诡异.于是简单了解了一下写成此博客. [写在前面] ...
- 【安卓】aidl.exe E 10744 10584 io_delegate.cpp:102] Error while creating directories: Invalid argument
这几天在使用.aidl文件的时候eclipse的控制台总是爆出如下提示: aidl.exe E 10744 10584 io_delegate.cpp:102] Error while creatin ...
- Identify Memory Leaks in Visual CPP Applications —— VLD内存泄漏检测工具
原文地址:http://www.codeproject.com/Articles/1045847/Identify-Memory-Leaks-in-Visual-CPP-Applications 基于 ...
随机推荐
- MVC文件上传
这次来看下一个MVC网站上传文件的功能,其中上传用到uploadify这个jquery插件,网上还有很多类似的,哪种都无所谓,能实现功能便行,貌似uploadify官网上的这个插件是要付费的,这里就顺 ...
- CI框架的事务开启、提交和回滚
1.运行事务 $this->db->trans_start(); // 开启事务$this->db->query('一条SQL查询...');$this->db-> ...
- 分布式系统的烦恼------《Designing Data-Intensive Applications》读书笔记11
使用分布式系统与在单机系统中处理问题有很大的区别,分布式系统带来了更大的处理能力和存储容量之后,也带来了很多新的"烦恼".在这一篇之中,我们将看看分布式系统带给我们新的挑战. 1. ...
- 交换机高级特性MUX VLAN
MUX VLAN 基本概念 lMUX VLAN(Multiplex VLAN)提供了一种通过VLAN进行网络资源控制的机制. 例如,在企业网络中,企业员工和企业客户可以访问企业的服务器. 对于企业来说 ...
- Win10如何配置Jdk环境变量
对于每一位做Java开发的朋友来说,Jdk是必须要安装的,安装好了Jdk,其实并没有结束,还需要配置Jdk的环境变量,系统在不断地更新,小编给大家介绍一下如何在Win10下配置Jdk,并检测是否配置成 ...
- C#拖拽操作
C#的拖拽 本文将以Winform为例 有两个主要的事件: DragEnter 拖拽到区域中触发的事件 DragDrop 当拖拽落下的时候出发此事件 饮水思源 参考博客: http://www.cnb ...
- [leetcode tree]96. Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- wmware虚拟系统光盘的问题
拿到系统盘,需要通过UltralSO工具中:工具-制作光盘映像文件,做成系统iso文件,而不是直接拷贝系统盘里的文件压缩成iso格式. 主要原因:主要是系统盘有一个引导区,win系统复制光盘时,是不能 ...
- luoguP3952 [NOIP2017]时间复杂度 模拟
原本只是想看下多久能码完时间复杂度 然后在30min内就码完了,然后一A了???? 首先,这题完全可以离线做 我们先把所有的操作读完,判断合不合法之后,再去判断和标准答案的关系 具体而言 把所有的操作 ...
- Windows下修改Git bash的HOME路径
Windows中使用http://git-scm.com/安装Git bash工具,默认的HOME和~路径一般都是C:\Users\用户名,每次得用命令切换到常用的Repository下,此操作重复而 ...