惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
J
Java Code Geeks
博客园 - 【当耐特】
宝玉的分享
宝玉的分享
腾讯CDC
D
DataBreaches.Net
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
V
V2EX
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
Jina AI
Jina AI
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
B
Blog
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium

博客园 - 周伟

域名解析文件hosts文件是什么?如何修改hosts文件? PowerDesigner 使用心得 实现TOMCAT服务下一个ip绑定多域名绑定的方法 C++导入导出CSV文件 struct+vector实现树结构 WebService 随写 mysql相关文章收集 Linux下两种自动启动Tomcat的方法 linux自动启动mysql LINUX下mysql的大小写区分设置 Jdom使用指南 vector+struct在数据接口中的应用 UML用例中使用和扩展意义 枚举和字符串互转 单件模式的C++标准实现 设计模式 Add Crash Reporting to Your Applications with the CrashRpt Library 设计模式——单键模式(singleton)C++实现 Singleton的C++实现 及相关问题
CSV扩展类
周伟 · 2009-09-04 · via 博客园 - 周伟

#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
typedef std::vector<std::string> row_t;

class CCsvEx
{
public:
 CCsvEx(void);
 virtual ~CCsvEx(void);
 
 bool Open(string sFileName,char cDelimiter);

 void Clear();

 bool GetList(string sFileName,char cDelimiter,vector<row_t> &list);

 void CSVLine_Populate(row_t &row, const string& line, char delimiter);

 unsigned long GetRowCount();

 const std::string GetItemValue(unsigned long row,
  unsigned int index);

 const std::string GetItemString(unsigned long row,
  unsigned int index);

 float GetItemFloat(unsigned long row,
  const unsigned int index);

 long GetItemLong(unsigned long row,
  const unsigned int index);

 SYSTEMTIME GetItemSTTime(unsigned long row,
  const unsigned int index);


private:
 row_t * FindRow(unsigned long findrow);
 std::vector<row_t> m_recordset;
};

#include "StdAfx.h"
#include ".\csvex.h"

CCsvEx::CCsvEx(void)
{
}

CCsvEx::~CCsvEx(void)
{
}

bool CCsvEx::Open(string sFileName,char cDelimiter)
{
 string line;
 ifstream in(sFileName.c_str());
 if (in.fail()) 
 {
  return false;
 }
 m_recordset.clear();
 row_t row;
 while(getline(in, line)  && in.good())
 {
  row.clear();
  this->CSVLine_Populate(row, line, cDelimiter);
  m_recordset.push_back(row);
 }
 in.close();
 return true;
}

void CCsvEx::CSVLine_Populate(row_t &row, const string& line, char delimiter)
{
 int linepos=0;
 int inquotes=false;
 char c;
 int i;
 int linemax=line.length();
 string curstring;
 row.clear();

 while(line[linepos]!=0 && linepos < linemax)
 {

  c = line[linepos];

  if (!inquotes && curstring.length()==0 && c=='"')
  {
   //beginquotechar
   inquotes=true;
  }
  else if (inquotes && c=='"')
  {
   //quotechar
   if ( (linepos+1 <linemax) && (line[linepos+1]=='"') )
   {
    //encountered 2 double quotes in a row (resolves to 1 double quote)
    curstring.push_back(c);
    linepos++;
   }
   else
   {
    //endquotechar
    inquotes=false;
   }
  }
  else if (!inquotes && c==delimiter)
  {
   //end of field
   row.push_back( curstring );
   curstring="";
  }
  else if (!inquotes && (c=='\r' || c=='\n') )
  {
   row.push_back( curstring );
   return;
  }
  else
  {
   curstring.push_back(c);
  }
  linepos++;
 }
 row.push_back( curstring );
 return;
}

unsigned long CCsvEx::GetRowCount()
{
 return m_recordset.size();
}

row_t * CCsvEx::FindRow(unsigned long findrow)
{
 if(findrow >= this->GetRowCount() || m_recordset.size() == 0)
 {
  return NULL;
 }

 return &m_recordset[findrow];
}

const std::string CCsvEx::GetItemValue(unsigned long row,
           unsigned int index)
{
 row_t * rowvalue = FindRow(row);
 if(rowvalue ==  NULL)
 {
  return "";
 }

 return (*rowvalue)[index];
}

const std::string CCsvEx::GetItemString(unsigned long row,
        unsigned int index)
{
 return GetItemValue(row,index);
}

float CCsvEx::GetItemFloat(unsigned long row,
       const unsigned int index)
{
 return atof(GetItemValue(row,index).c_str());
}

long CCsvEx::GetItemLong(unsigned long row,
     const unsigned int index)
{
 return atol(GetItemValue(row,index).c_str());
}

SYSTEMTIME CCsvEx::GetItemSTTime(unsigned long row,
       const unsigned int index)
{
 SYSTEMTIME sysTime;
 try
 {
  COleDateTime oleTime;
  oleTime.ParseDateTime(GetItemValue(row,index).c_str()); 
  VariantTimeToSystemTime(oleTime, &sysTime);
 }
 catch (...)
 {
  GetSystemTime(&sysTime);
 }
 return sysTime;
}

bool CCsvEx::GetList(string sFileName,char cDelimiter,vector<row_t> &list)
{
 string line;
 ifstream in(sFileName.c_str());
 if (in.fail()) 
 {
  return false;
 }
 row_t row;
 while(getline(in, line)  && in.good())
 {
  row.clear();
  this->CSVLine_Populate(row, line, cDelimiter);
  list.push_back(row);
 }
 in.close();
}