从制表符分隔的文本文件中读取单个列

时间:2014-06-04 09:29:00

标签: c++ qt

这里是制表符分隔文件的一个示例(部分),我想从中提取列以进行单独的详细说明:

 0.0000000000000000e+000     -2.2562500000000000e+001    -2.0625000074505806e-001    -1.0000000149011612e-001    -3.0625000223517418e-001   
  9.9999999392252903e-009    -2.5687500000000000e+001    -2.0625000074505806e-001    -1.0000000149011612e-001    -3.0625000223517418e-001   
  1.9999999878450581e-008    -3.1937500000000000e+001    -3.6250000074505806e-001     5.6249998509883881e-002    -3.0625000223517418e-001   
  2.9999999817675871e-008    -3.3500000000000000e+001    -3.6250000074505806e-001     5.6249998509883881e-002    -3.0625000223517418e-001   
  3.9999999756901161e-008    -3.1937500000000000e+001    -2.0625000074505806e-001     5.6249998509883881e-002    -1.5000000223517418e-001   
  4.9999999696126451e-008    -3.0375000000000000e+001     1.0624999925494194e-001     5.6249998509883881e-002     1.6249999776482582e-001   
  5.9999999635351742e-008    -2.4125000000000000e+001     1.0624999925494194e-001    -1.0000000149011612e-001     6.2499977648258209e-003   
  6.9999999574577032e-008    -1.9437500000000000e+001     1.0624999925494194e-001    -1.0000000149011612e-001     6.2499977648258209e-003   
  7.9999999513802322e-008    -1.9437500000000000e+001     1.0624999925494194e-001    -2.5625000149011612e-001    -1.5000000223517418e-001   
  8.9999999453027613e-008    -1.9437500000000000e+001     2.6249999925494194e-001    -2.5625000149011612e-001     6.2499977648258209e-003   
  9.9999999392252903e-008    -1.1625000000000000e+001     2.6249999925494194e-001    -1.0000000149011612e-001     1.6249999776482582e-001   
  1.0999999933147819e-007     4.0000000000000000e+000     2.6249999925494194e-001    -1.0000000149011612e-001     1.6249999776482582e-001   
  1.1999999927070348e-007     1.1812500000000000e+001     1.0624999925494194e-001    -1.0000000149011612e-001     6.2499977648258209e-003   
  1.2999999920992877e-007     8.6875000000000000e+000     1.0624999925494194e-001    -2.5625000149011612e-001    -1.5000000223517418e-001   
  1.3999999914915406e-007     8.6875000000000000e+000    -5.0000000745058060e-002     5.6249998509883881e-002     6.2499977648258209e-003   
  1.4999999908837935e-007     1.3375000000000000e+001    -5.0000000745058060e-002    -1.0000000149011612e-001    -1.5000000223517418e-001   
  1.5999999902760464e-007     1.1812500000000000e+001    -5.0000000745058060e-002    -1.0000000149011612e-001    -1.5000000223517418e-001   
  1.6999999896682993e-007     8.6875000000000000e+000    -2.0625000074505806e-001     5.6249998509883881e-002    -1.5000000223517418e-001   
  1.7999999890605523e-007     8.6875000000000000e+000    -2.0625000074505806e-001     2.1249999850988388e-001     6.2499977648258209e-003   
  1.8999999884528052e-007     1.3375000000000000e+001    -3.6250000074505806e-001     5.6249998509883881e-002    -3.0625000223517418e-001   
  1.9999999878450581e-007     8.6875000000000000e+000    -3.6250000074505806e-001     5.6249998509883881e-002    -3.0625000223517418e-001

从这些数据中,我想提取例如第二列和第四列。 这样做的简单方法(代码)是什么?

提前致谢:)

这是打开文件的部分(qt框架):

void MainWindow::on_Button_FirstColumn_clicked()
{
    QFile dataAG("C:/Users/Marco/Desktop/Segmentazione+GUI_GFAC/20130909_LT25A_E21_H20_1/data_t_U_I_test.txt");
    if(dataAG.open(QIODevice::ReadOnly | QIODevice::Text))
    {


    QTextStream stream(&dataAG);
    //QString line;
    ui->textBrowser->setText(stream.readAll());

    dataAG.close();
}

}

这是读取列的代码:

void MainWindow::on_LoadVoltageButton_clicked()
{   
    float  col1[74],col2[74],col3[74],col4[74],col5[74];
    int num=0;
    string Load;
    ifstream dataAgie;
    dataAgie.open("C:/Users/Marco/Desktop/Segmentazione+GUI_GFAC/20130909_LT25A_E21_H20_1/data_t_U_I_test.txt");
    if(dataAgie.fail()) // checks to see if file opended 
    { 
      QMessageBox::information(this,tr("STOP"),tr("Error with loading"));
      //this->close();
      } 
    while(getline(dataAgie,Load)) // reads file to end of *file*, not line
      {  
         istringstream is(Load);
         double P;
         double Q;
         int n=0;
         is >> P >> Q;

      } 

}

2 个答案:

答案 0 :(得分:2)

vector<float> column1;
vector<float> column2;
vector<float> column3;
float temp1;
float temp2;
float temp3;

ifstream file("numbers");

while ((file >> temp1) && (file >> temp2) && (file >> temp3))
{
  column1.push_back(temp1);
  column2.push_back(temp2);
  column3.push_back(temp3);
}

不是最快的解决方案,而是简短而简单。

答案 1 :(得分:1)

使用我创建here的列迭代器,你可以简单地执行此操作:

int main() {
    auto c2 = read_columns<double>(is, 2);
    auto c4 = read_columns<double>(is, 4)
}

您甚至可以在地图中保留列以便于访问:

std::map<int, std::vector<double>> columns;

columns.emplace(std::make_pair(2, c2));
columns.emplace(std::make_pair(4, c4));