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

推荐订阅源

S
Security Affairs
S
Secure Thoughts
P
Proofpoint News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Schneier on Security
V
Vulnerabilities – Threatpost
Security Archives - TechRepublic
Security Archives - TechRepublic
T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
Latest news
Latest news
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
T
Troy Hunt's Blog
H
Heimdal Security Blog
美团技术团队
Webroot Blog
Webroot Blog
P
Proofpoint News Feed
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
P
Privacy & Cybersecurity Law Blog
U
Unit 42
Google DeepMind News
Google DeepMind News
V2EX - 技术
V2EX - 技术
G
Google Developers Blog
N
News and Events Feed by Topic
Project Zero
Project Zero
The Register - Security
The Register - Security
N
Netflix TechBlog - Medium
IT之家
IT之家
月光博客
月光博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
L
Lohrmann on Cybersecurity
Schneier on Security
Schneier on Security
博客园_首页
Help Net Security
Help Net Security
AWS News Blog
AWS News Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Security @ Cisco Blogs
PCI Perspectives
PCI Perspectives
Cisco Talos Blog
Cisco Talos Blog
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
阮一峰的网络日志
阮一峰的网络日志
Spread Privacy
Spread Privacy
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Hacker News
The Hacker News

博客园 - 周伟

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