.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; }