C++写的一个读取CSV文件的函数

毕业就没有再写过C++程序,真有点遗憾。
今天恰好做一个东西需要一个C++读取CSV文件的函数。
没有找到现成的函数,所以就自己写了一个。
目的就是将csv文件的内容读取出来,每一行读到一个数组中。

有点生疏了,字符串处理函数都不知道有什么现成的,所以也写了两个辅助的字符串处理函数。

第一个函数:计算一个字符串中的某个子串的个数
ExpandedBlockStart.gif ContractedBlock.gif int  strstr_cnt( const   char   * string const   char   * substring)  dot.gif {
InBlock.gif 
int i,j,k,count = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif 
for (i = 0string[i]; i++)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  
for (j = i, k = 0; (string[j] == substring[k] && (j < strlen(string))); j++,k++dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
if (! substring[k + 1]) dot.gif{
InBlock.gif    count
++;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif 
return count;
ExpandedBlockEnd.gif}
第二个函数:计算一个子串在字符串中的位置
ExpandedBlockStart.gif ContractedBlock.gif int  substring_index( const   char   * s1, const   char   * s2,  int  pos) dot.gif {
InBlock.gif    
int i,j,k;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for( i = pos ; s1[i] ; i++ ) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for( j = i, k = 0 ; s1[j] == s2[k]; j++,k++ )dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (! s2[k + 1]) dot.gif{
InBlock.gif                
return i;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return -1;
ExpandedBlockEnd.gif}
第三个函数:读取已经打开的一个CSV文件的一行,将这一行处理到一个数组中。
ExpandedBlockStart.gif ContractedBlock.gif char   * fgetcsvline(vector < string >   & csv_databuf, FILE  * fhead)  dot.gif {
InBlock.gif    
char  *ret_stat;
InBlock.gif    
char  data_buf[1024];
InBlock.gif    
string stringbuf;
InBlock.gif    ret_stat 
= fgets( data_buf, 1024, fhead );
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (ret_stat != NULL) dot.gif{
InBlock.gif        
int len = strstr_cnt(data_buf,"\",\"");
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (len > 0)dot.gif{
InBlock.gif            
int pos = substring_index(data_buf,"\",\"",0);
InBlock.gif            
int startpos = 1;
InBlock.gif            
string csv_buf;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
while (pos > 0dot.gif{
InBlock.gif                stringbuf 
= (string)data_buf;
InBlock.gif                csv_buf 
= stringbuf.substr(startpos,pos - startpos);
InBlock.gif                csv_databuf.push_back(csv_buf);
InBlock.gif                startpos 
= pos + 3;
InBlock.gif                pos 
= substring_index(data_buf,"\",\"",pos + 2);
ExpandedSubBlockEnd.gif            }

                if ((substring_index(data_buf,"\n",0)) > 0){
                       csv_buf = stringbuf.substr(startpos, stringbuf.length() - startpos - 2);
                } else {
                       csv_buf = stringbuf.substr(startpos, stringbuf.length() - startpos - 1);
                }

InBlock.gif            csv_databuf.push_back(csv_buf);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return ret_stat;    
ExpandedBlockEnd.gif}
这个函数使用上面的两个函数来处理字符串。
另外这个函数用来处理的CSV文件是带双引号格式的:
"sss","ddd","444"
"ttt","www","ooo"
"sss","qqq","000"
使用方法如下:
ExpandedBlockStart.gif ContractedBlock.gif int  main( int  argc,  char *  argv[])  dot.gif {
InBlock.gif    FILE  
*fp_head;
InBlock.gif    
string csvFileName = "test.csv";
InBlock.gif    
char  *ret_stat;
InBlock.gif    vector
<string> csv_data;
InBlock.gif
InBlock.gif    fp_head 
= fopen( csvFileName, "rt" );
InBlock.gif    ret_stat = fgetcsvline(csv_data, fp_head);
ExpandedSubBlockStart.gifContractedSubBlock.gif    
while (ret_stat != NULL) dot.gif{
InBlock.gif        
//get csv data use csv_data[n]
InBlock.gif
        ret_stat = fgetcsvline(csv_data, fp_head);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

以上代码可能需要稍加调试。
也可稍加改动用来读取其它格式的csv文件。

转载于:https://www.cnblogs.com/c-delight/archive/2005/12/12/295564.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值