PDA

View Full Version : ReadMultiple Files at one time



giblit
24th March 2013, 06:06
First of all, I couldnt figure out how to map or get an array of variable names basically I was trying like QFile file[i] where I would be like 1-3 and i wanted it to be QFile file1, QFile2, QFile3 same with QTextStream because I think if I had that working it would be a lot better.

Secondly with my Code below I can read in two files, but if I try and read in a 3rd I get an application failure and it wont even open the exe get this error message:
ASSERT failure in QLIST<T>::operator[]: "index out of range", file C:\QtSDK\Desktop\Qt\4.8.1\mingw\include/Qtcore/qlist.h, line 477. and two mesages saying Invalid parameter passed to C runtime.

my code is here PS: if i make my two loops i < 2 and comment out the other 3 files it works but I want to read all 5 =/

#include <QtGui>
#include "datelistdialog.h"
#include "abitem.h"
#include <sstream>
#include <iostream>
using namespace std;
DateListDialog::DateListDialog()
{

QStringList foods;
QStringList dates;
QStringList totalProteins;
QStringList totalFats;
QStringList totalCarbs;
QStringList labels;
labels << tr("Date") << tr("Food") << tr("Total Protein") << tr("Total Carb") << tr("Total Fat");

treeWidget = new QTreeWidget;
treeWidget->setColumnCount(5);
treeWidget->setHeaderLabels(labels);



QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(treeWidget);
setLayout(mainLayout);

for(int i = 0; i < 3; i++){

string filename = "/";
string type [] = {"date","food","totalProtein","totalCarb","totalFat"};
string number;
stringstream convert;
filename.append(type[i]);
convert << i;
number = convert.str();
filename.append(".txt");



QString filename2 = QString::fromStdString(filename);
QFile i(QDir::homePath() + filename2);
if (i.open(QIODevice::ReadOnly)) {

QTextStream input(&i);
input.setCodec("UTF-8");
while(!input.atEnd()){

if(filename == "/date.txt"){
dates << input.readLine();
} else if(filename == "/food.txt"){
foods << input.readLine();
} else if(filename == "/totalProtein.txt"){
totalProteins << input.readLine();
} else if(filename == "/totalCarb.txt"){
totalCarbs << in.readLine();
in.readLine();
} else if(filename == "/totalFat.txt"){
totalFats << in.readLine();
in.readLine();
}
}
}
}



for (int i = 0; i < 5; ++i) {
QTreeWidgetItem* item = new QTreeWidgetItem;
//item->setText(0,"HELLO");
item->setText(0, dates[i]);
item->setText(1, foods[i]);
item->setText(2, totalProteins[i]);
item->setText(3, totalCarbs[i]);
item->setText(4, totalFats[i]);
treeWidget->addTopLevelItem(item);

}
}

anda_skoa
24th March 2013, 13:47
my guess is that you get the assert when accessing the QStringLists in line 71 and later. Maybe you should check that they all have 6 entries.

Also: naming the QFile instance with the same name as the loop variable looks very weird.

Btw, the easiest way to create filenames based on a patter is QString::arg(). Basically you create a QString using placeholders and then use arg() calls to replace them with data



const QString filenamePattern( "/%1%2.txt" );

QString filename = filenamePattern.arg( type[i] ).arg( i );


Cheers,
_

giblit
24th March 2013, 20:47
thanks about the arg that will make life much easier, and btw you were correct about the line 71 and later..I did a .size() check on all of them and found that one of my txt files was only one line instead of 4 like it should have been reason for this was when I was writing the file I forgot the output << endl; (well I had it but it was commented out on that one instead of all the others =p) Thanks again.
EDIT: the reading of multiple files was working correctly whole time like you thought.

ChrisW67
24th March 2013, 23:43
Line 42 should be generating a warning that QFile i masks the int i loop counter (line 28) also.

giblit
25th March 2013, 00:10
I didn't get any errors but I can rename QFile i to QFile file. tbh I was tyring to make it Qfile 1, Qfile 2, ect by using the i but it just made the variable name i and it works having the same variable name for them all.

The program is working perfectly now though btw