C++处理CSV


来自:http://www.zedwood.com/article/112/cpp-csv-parser

C++ CSV Parser

 

The function featured here is of course csvline_populate. It parses a line of data by a delimiter. If you pass in a comma as your delimiter it will parse out a Comma Separated Value (CSV) file. If you pass in a '\t' char it will parse out a tab delimited file (.txt or .tsv). CSV files often have commas in the actual data, but accounts for this by surrounding the data in quotes. This also means the quotes need to be parsed out, this function accounts for that as well.

It would make some sense to only pass in the line and delimiter to the function, and have the return type be the vector. However in terms of performance under heavy loads, this makes less sense. Passing in a predefined vector allows a function to populate it, copying bytes from your line as it goes. However, to not pass in a predefined vector, we'd have to declare it as a local variable to the function, which declares it on the stack. This means when the function completes, the variable will be deallocated, so when it returns the vector, the return keyword uses the copy constructor of the vector (which uses the copy constructor of each string object) to assign the return type to the variable in the caller function. To copy the return value of an object (depending on the size of your vector and the number of times you do it), can be an expensive operation.

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

using namespace std;

void csvline_populate (vector <string > &record, const string & line, char delimiter );

int main ( int argc, char *argv [ ] )
{
vector <string > row;
string line;
ifstream in ( "input.csv" );
if (in. fail ( ) )   { cout << "File not found" <<endl; return 0; }

while (getline (in, line )   && in. good ( ) )
{
csvline_populate (row, line, ',' );
for ( int i = 0, leng =row. size ( ); i <leng; i ++ )
cout << row [i ] << "\t";
cout << endl;
}
in. close ( );
return 0;
}

void csvline_populate (vector <string > &record, const string & line, char delimiter )
{
int linepos = 0;
int inquotes =false;
char c;
int i;
int linemax =line. length ( );
string curstring;
record. 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
record. push_back ( curstring );
curstring = "";
}
else if ( !inquotes && (c == '\r' || c == '\n' ) )
{
record. push_back ( curstring );
return;
}
else
{
curstring. push_back (c );
}
linepos ++;
}
record. push_back ( curstring );
return;
}


Note:
The only other problem with this code here, is that the definition of a csv allows for the newline character '\n' to be part of a csv field if the field is surrounded by quotes. The csvline_populate function takes care of this properly, but the main function which calls csvline_populate doesn't handle it. The way the main function works is that the getline(in, line) function populates the next line of the file from the 'in' stream using '\n' as a delimiter. So, if there is a '\n' in the middle of the csv field, the getline(in,line) still treats it as an end of line character. Most CSV files do not have a \n in the middle of the field, so it is usually not worth worrying about.

If one were to fix this issue, it would be by making a fgetcsv type function which operates directly on the FILE* handle or the ifstream. The function would also have to detect EOF (end of file).
 

类别: c++  查看评论

转载于:https://my.oschina.net/zhmsong/blog/5234

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值