简体   繁体   中英

Qt read specific columns of tab delimited text file

I'm working on a program that reads a text file of tab delimited doubles and sums up each column, effectively calculating the integral of each column.

What I want to be able to do is choose a specific column to sum up rather than sum all the columns.

The code I've already got working for the integral of all columns:

void MainWindow::on_pushButton_clicked()
{


    data::SingleLineData.resize(512);
    QString test;
    QString inputfile = QFileDialog::getOpenFileName(
                this,
                tr("Open File"),
                "/Users",
                "All files (*.*)"
                );

    if(inputfile != ""){
    QFile file(inputfile);


    if(!file.open(QFile::ReadOnly)){
       }
    QTextStream in(&file);


        double buffer;

        while(!file.atEnd()){
            in.readLine();
            for(int i=0; i<512; i++){
                in >> buffer;
                data::SingleLineData[i]+=buffer;
            }
        }

    }
        qDebug() << data::SingleLineData;
// ************* file output **************************************************

        QString filename = QFileDialog::getSaveFileName(
                    this,
                    tr("Save File"),
                    "/Users",
                    "Text files (*.txt)"
                    );
        QFile fileout(filename);
        if (fileout.open(QFile::ReadWrite | QFile::Truncate)){
         QTextStream out(&fileout);
         for (QVector<double>::iterator iter = data::SingleLineData.begin(); iter != data::SingleLineData.end(); iter++){
             out << *iter <<", ";
         }
         fileout.close();
        }
}

And here is my attempt at choosing out an individual column using an integer input into a spin box:

void MainWindow::on_pushButton_2_clicked()
{
    QString inputfile = QFileDialog::getOpenFileName(
                this,
                tr("Open File"),
                "/Users",
                "All files (*.*)"
                );
    QVector<double> SingleChannel;

    if (inputfile != ""){
        QFile file(inputfile);

        if (!file.open(QIODevice::ReadOnly)){
        }
        QTextStream in(&file);
        SingleChannel.resize(1);
        double buffer;
        int channelnumber = ui->spinBox->value();
        while(!file.atEnd()){
            in.readLine();
            for (int i = 0; i < 512; i++){
                in >> buffer;
                if (i == channelnumber){
                    SingleChannel.push_back(buffer);
                }
                data::SingleLineData[i]+=buffer;
            }
        }
    }
    qDebug() << SingleChannel;
}

I get the following error though:

ASSERT failure in QVector<T>::operator[]: "index out of range", file /Users/mduncan/Qt/5.3/clang_64/lib/QtCore.framework/Headers/qvector.h, line 385
The program has unexpectedly finished.

Anyone have any ideas?

Thanks :)

add this line at the beginning of void MainWindow::on_pushButton_2_clicked method

data::SingleLineData.resize(512); 

this will resize your container to 512 defaulted value count elements. Because you use data::SingleLineData[i]+=buffer, which need access the i th element in the container.

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