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

推荐订阅源

V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
The Last Watchdog
The Last Watchdog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Secure Thoughts
Hacker News - Newest:
Hacker News - Newest: "LLM"
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
TaoSecurity Blog
TaoSecurity Blog
博客园 - Franky
有赞技术团队
有赞技术团队
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
NISL@THU
NISL@THU
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
W
WeLiveSecurity
SecWiki News
SecWiki News
Google DeepMind News
Google DeepMind News
O
OpenAI News
V
V2EX
AI
AI
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
美团技术团队
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 三生石上(FineUI控件)
G
GRAHAM CLULEY
月光博客
月光博客
T
Threatpost
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Vulnerabilities – Threatpost
Last Week in AI
Last Week in AI
爱范儿
爱范儿
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cloudbric
Cloudbric
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
N
News | PayPal Newsroom
Know Your Adversary
Know Your Adversary

博客园 - 周伟

域名解析文件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();
}