C++ 读取.tsv文件中的特定列
我正在尝试将.tsv文件中的数据读入向量。该文件的结构如下所示:C++ 读取.tsv文件中的特定列,c++,csv,C++,Csv,我正在尝试将.tsv文件中的数据读入向量。该文件的结构如下所示: A B 1 2 2 4 6 8 我想询问程序应该读取哪一列,然后将该列中的值推送到向量中。到目前为止,我的代码是: int main() { string filename, column, headerLine; vector<double> data, tempData; int numColumn; ifstream dataFile; cout << "enter a
A B
1 2
2 4
6 8
我想询问程序应该读取哪一列,然后将该列中的值推送到向量中。到目前为止,我的代码是:
int main() {
string filename, column, headerLine;
vector<double> data, tempData;
int numColumn;
ifstream dataFile;
cout << "enter a filename" << endl;
cin >> filename;
dataFile.open(filename);
cout << "enter a column name" << endl;
cin >> column;
cout << "reading column " << column << " from " << filename;
getline(dataFile, headers);
//here's where I feel stuck
}
intmain(){
字符串文件名、列、标题行;
矢量数据,tempData;
国际数列;
ifstream数据文件;
cout文件名;
打开(文件名);
cout柱;
请尝试以下代码:
while(!dataFile.eof()) {
std::string str;
std::getline( dataFile, str);
std::stringstream buffer(str);
std::string temp;
std::vector<double> values;
while( getline( buffer, temp, '\t') ) {
values.push_back( ::strtod(temp.c_str(), 0));
}
data.push_back(values[numColumn]);
}
while(!dataFile.eof()){
std::字符串str;
std::getline(数据文件,str);
std::stringstream缓冲区(str);
std::字符串温度;
std::向量值;
while(getline(buffer,temp,'\t')){
value.push_back(::strtod(temp.c_str(),0));
}
数据。推回(值[numColumn]);
}
是否只有两列?每个标题的名称是否唯一?我有两个以上的列,是的,每个标题的名称都是唯一的。列的名称的长度也可以超过1个字符,但不会包含空格。标题的数据类型似乎没有问题,因为您正在比较字符串,我现在回答您的问题。
while(!dataFile.eof()) {
std::string str;
std::getline( dataFile, str);
std::stringstream buffer(str);
std::string temp;
std::vector<double> values;
while( getline( buffer, temp, '\t') ) {
values.push_back( ::strtod(temp.c_str(), 0));
}
data.push_back(values[numColumn]);
}