getline()函数将标题组合在一起

时间:2012-02-03 13:49:42

标签: c++ header getline tab-delimited-text

我的数据文件的第一行是三(或四)列数据的标题:

%b02_a08    b02_a08_counts  b02_a08_eu

如您所见,列是TAB分隔的。当我使用getline()时,它会将整条线混合在一起。如果我使用getline(,,'\t'),则会返回该行的%b02_a08。我需要将行分开,因为我读了每个标题。

如果有人知道从数据文件中读取标题的方法并将它们放入vector<string>以便我可以在它们上运行Regex以找到其中包含“eu”的那个,那就是我需要做。那么,那就读取数据列,我可能会遇到相同的TAB分隔问题。

1 个答案:

答案 0 :(得分:0)

使用流卢克......

,如,

// process the header
std::getline(some_input_stream, line))
{
  std::istringstream str(line);
  // Now reaad each field
  str >> header_1 >> header_2 >> header_3;
  if (str >> header_4)
  {
    // process the optional column
    have_col_4 = true;
  }
}

// Now to read the rows
while(some_input_stream >> col_1 >> col_2 >> col_3)
{
  if (have_col_4)
    some_input_stream >> col_4;
}

最后一个块也可以用std::getline完成(即读取一行,构造一个流,然后从流中读取),但这往往会慢一些......

相关问题