[英]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. 我正在开发一个程序,该程序读取制表符分隔的double的文本文件并汇总每一列,从而有效地计算每一列的积分。
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 在void MainWindow :: on_pushButton_2_clicked方法的开头添加此行
data::SingleLineData.resize(512);
this will resize your container to 512 defaulted value count elements. 这会将您的容器大小调整为512个默认值计数元素。 Because you use data::SingleLineData[i]+=buffer, which need access the i th element in the container. 因为您使用data :: SingleLineData [i] + = buffer,所以需要访问容器中的第i个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.