练习写C++代码(111)-读入.csv文件

本文介绍了一个简单的C++程序,该程序能够读取两个CSV文件中指定列的数据,并计算这两列数值之间的差值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.csv文件是由逗号分割的文件,可以当作纯文本来处理。

下面写的是一个小程序,读入两个.csv文件,并指定要处理的列数,程序将两列提取出来,并计算相减的结果,并输出。


///read.cpp

#include
#include
#include
#include
#include
#include
using namespace std;

///passed a filename like *.csv and the index of column
///and return the column what you select
vector getValue(char *, const int);

int main(int argc, char** argv)
{
	//the index of column
	const int NO = atoi(argv[3]);
	vector svec1 = getValue(argv[1], NO);
	vector svec2 = getValue(argv[2], NO);
	vector ivec;
	
	//sub two column and assign to ivec
	for (vector::iterator it1 = svec1.begin(),it2 = svec2.begin(); it1 != svec1.end(),it2 != svec2.end(); ++it1, ++it2)
	{
		ivec.push_back(atoi((*it1).c_str()) - atoi((*it2).c_str()));
	}

	//output
	for (vector::iterator it = ivec.begin(); it != ivec.end(); ++it)
	{
		cout<<*it< getValue(char * filename, const int NO)
{
	ifstream file (filename);
	vector svec;
	string line;
	string value;
	int count;
	//read a line 
	while (getline(file,line))
	{
		count = 1;
		stringstream ss(line);
		//get the string with the NO
		while (getline(ss,value,',') && count != NO)
		{
			count++;
		}
		svec.push_back(value);
	}	
	return svec;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值