Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 读取.tsv文件中的特定列_C++_Csv - Fatal编程技术网

C++ 读取.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

我正在尝试将.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 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]);
}