简体   繁体   中英

Reading single columns from tab delimited text file

here an example (a part) of the tab delimited file from which I want to extract columns for separated elaborations:

 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

from this data, I want to extract for example the second and the fourth columns. What is a simple way (code) to do that?

Thanks in advance :)

This is the part to open the file (qt framework):

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();
}

}

this is the code to read columns:

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;

      } 

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

Not the fastest solution but short and simple.

Using the column iterator I made here you can simply do this:

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

You can even hold the columns in a map for easy access:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM