c++读csv

csvparser.h
#ifndef __CSVPARSE_H_2001_06_07__
#define __CSVPARSE_H_2001_06_07__


#include <string>
using namespace std;

class CSVParser {
 private:
  string m_sData;
  string::size_type m_nPos;
  void SkipSpaces(void);
 public:
  CSVParser();
  const CSVParser & operator << (const string &sIn);
  const CSVParser & operator << (const char *sIn);
  CSVParser & operator >> (int &nOut);
  CSVParser & operator >> (double &nOut);
  CSVParser & operator >> (string &sOut);
};

#endif


csvparser.cpp

 
#pragma once
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include "csvparser.h"
using namespace std;


CSVParser::CSVParser()
{
  m_sData = "";
  m_nPos = 0;
}

void CSVParser::SkipSpaces(void)
{
  while (m_nPos < m_sData.length() && m_sData[m_nPos] == ' ')
    m_nPos++;
}

const CSVParser & CSVParser::operator <<(const string & sIn)
{
  this->m_sData = sIn;
  this->m_nPos = 0;
  return *this;
}

const CSVParser & CSVParser::operator <<(const char *sIn)
{
  this->m_sData = sIn;
  this->m_nPos = 0;
  return *this;
}

CSVParser & CSVParser::operator >>(int & nOut)
{
  string sTmp = "";
  SkipSpaces();
  while (m_nPos < m_sData.length() && m_sData[m_nPos] != ',')
    sTmp += m_sData[m_nPos++];

  m_nPos++; // skip past comma
  nOut = atoi(sTmp.c_str());
  return *this;
}

CSVParser & CSVParser::operator >>(double & nOut)
{
  string sTmp = "";
  SkipSpaces();
  while (m_nPos < m_sData.length() && m_sData[m_nPos] != ',')
    sTmp += m_sData[m_nPos++];

  m_nPos++; // skip past comma
  nOut = atof(sTmp.c_str());
  return *this;
}

CSVParser & CSVParser::operator >>(string & sOut)
{
  bool bQuotes = false;
  sOut = "";
  SkipSpaces();

  // Jump past first " if necessary
  if (m_nPos < m_sData.length() && m_sData[m_nPos] == '"') {
    bQuotes = true;
    m_nPos++; 
  }
  
  while (m_nPos < m_sData.length()) {
    if (!bQuotes && m_sData[m_nPos] == ',')
      break;
    if (bQuotes && m_sData[m_nPos] == '"') {
      if (m_nPos + 1 >= m_sData.length() - 1)
        break;
      if (m_sData[m_nPos+1] == ',')
        break;
    }
    sOut += m_sData[m_nPos++];
  }

  // Jump past last " if necessary
  if (bQuotes && m_nPos < m_sData.length() && m_sData[m_nPos] == '"')
    m_nPos++; 

  // Jump past , if necessary
  if (m_nPos < m_sData.length() && m_sData[m_nPos] == ',')
    m_nPos++; 

  return *this;
}



调用方法:

// csv_test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <string>
#include "csvparser.h"

using namespace std;

int main(void)
{
	ifstream infile("sample.csv");
	string sLine;
	string sCol1, sCol3, sCol4;
	double fCol2;
	int iCol5, iCol6;

	CSVParser parser;

	while (!infile.eof()) {
		getline(infile, sLine); // Get a line
		if (sLine == "")
			continue;

		parser << sLine; // Feed the line to the parser

		// Now extract the columns from the line
		parser >> sCol1 >> fCol2 >> sCol3;
		parser >> sCol4 >> iCol5;
		parser >> iCol6;

		cout << "Column1: " << sCol1 << endl
			<< "Column2: " << fCol2 << endl
			<< "Column3: " << sCol3 << endl
			<< "Column4: " << sCol4 << endl
			<< "Column5: " << iCol5 << endl
			<< "Column6: " << iCol6 << endl
			<< endl;
	}
	infile.close();


	system("pause");
	return 0;
}



csv文件

sample.csv



你可能感兴趣的:(csv)