QString Split() issue.
-
Hi,
Trying to take a QString line, and split it into a QString value and a bunch of integer values.
When trying to split the values and convert certain ones to integer, it throws an "Index out of range" error:
ASSERT failure in QList<T>::operator[]: "index out of range"
void MainWindow::loadVec(QString linest) { containStuff temp; QStringList strTmp; QString dat3; int one1 , two2, three3, four4, five5, six6; strTmp = linest.split(QRegExp(",")); int one1, two2, three3, four4, five5, six6; dat3 = strTmp[0]; one1 = strTmp[1].toInt(); two2 = strTmp[2].toInt(); three3 = strTmp[3].toInt(); four4 = strTmp[4].toInt(); five5 = strTmp[5].toInt(); six6 = strTmp[6].toInt(); temp.loadDate(dat3); temp.loadFirst(one1); temp.loadSecond(two2); temp.loadThird(three3); temp.loadFourth(four4); temp.loadFifth(five5); temp.loadSecond(six6); fileContents.push_back(temp); return; }
However, when I try a small side example, using basically the same code, it works fine.
Side code (that works) as follows:#include <QCoreApplication> #include <iostream> int main(int argc, char *argv[]) { QStringList tmp; QCoreApplication a(argc, argv); QString line = "45,3,32,42,15"; int arrstuff[5]; std::cout << line.toStdString() << std::endl << std::endl; tmp = line.split(QRegExp(",")); arrstuff[0] = tmp[0].toInt(); std::cout << arrstuff[0] << " "; arrstuff[1] = tmp[1].toInt(); std::cout << arrstuff[1] << " "; arrstuff[2] = tmp[2].toInt(); std::cout << arrstuff[2] << " "; arrstuff[3] = tmp[3].toInt(); std::cout << arrstuff[3] << " "; arrstuff[4] = tmp[4].toInt(); std::cout << arrstuff[4] << " "; std::cout << std::endl; return a.exec(); }
I can make references to strTmp[0] but any references to strTmp[1] or above gives the "index out of range" error.
Can someone tell me what I am doing wrong?
Thanks,
Uberlinc.
-
@Uberlinc said in QString Split() issue.:
but any references to strTmp[1] or above gives the "index out of range" error
Well, the error says it all.
Your list only contains one element.
How does your input string look like?
Also, why do you use a regular expression if you want to split at ","? -
The data is read in from a file and it separated by commas.
If I reference strTmp[0], it correctly returns the first element of the the QString value linest.
The example below shows a shorter version that uses the exact same methodology and it works.
I can reference arrstuff[0] - arrstuff[4] happily without error.In the second example (the one that works) instead of using individual variables to hold the data (e.g. one1, two2, etc) I simply place them in an array of int.
It has no problem with this.
I only used QRegExp because I found this in my code that works, so added it to my main code to see if it makes a difference.
It didn't.Can you see the flaw in the top piece of code?
Thanks.
-
@Uberlinc said in QString Split() issue.:
Can you see the flaw in the top piece of code?
No.
You did not tell what EXACT input string you're splitting?
Why don't you check that? It probably does not contain any ',' -
Here is the file that I'm reading in from at its entirety:
20210101,23,27,44,1,41,18
20210107,20,44,20,31,13,34
20210114,23,24,36,28,5,8
20210121,23,33,44,8,38,22
20210128,18,6,16,30,17,34
20210204,29,8,20,3,4,12
20210211,11,28,37,38,5,9
20210218,34,4,32,25,40,3
20210225,26,34,25,31,38,5
20210304,31,3,41,28,12,2
20210311,37,39,41,28,26,19
20210318,2,11,38,43,38,23
20210325,45,7,8,37,14,12 -
@JonB said in QString Split() issue.:
tmp = line.split(","); qDebug() << line << tmp.length() << tmp;
22:05:08: Starting /home/linc/Documents/Workspace/Qt/build-SimpleFile-Desktop-Debug/SimpleFile... "20210107,20,44,20,31,13,34" 7 ("20210107", "20", "44", "20", "31", "13", "34") "20210114,23,24,36,28,5,8" 7 ("20210114", "23", "24", "36", "28", "5", "8") "20210121,23,33,44,8,38,22" 7 ("20210121", "23", "33", "44", "8", "38", "22") "20210128,18,6,16,30,17,34" 7 ("20210128", "18", "6", "16", "30", "17", "34") "20210204,29,8,20,3,4,12" 7 ("20210204", "29", "8", "20", "3", "4", "12") "20210211,11,28,37,38,5,9" 7 ("20210211", "11", "28", "37", "38", "5", "9") "20210218,34,4,32,25,40,3" 7 ("20210218", "34", "4", "32", "25", "40", "3") "20210225,26,34,25,31,38,5" 7 ("20210225", "26", "34", "25", "31", "38", "5") "20210304,31,3,41,28,12,2" 7 ("20210304", "31", "3", "41", "28", "12", "2") "20210311,37,39,41,28,26,19" 7 ("20210311", "37", "39", "41", "28", "26", "19") "20210318,2,11,38,43,38,23" 7 ("20210318", "2", "11", "38", "43", "38", "23") "20210325,45,7,8,37,14,12" 7 ("20210325", "45", "7", "8", "37", "14", "12") "" 1 ("") ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/x86_64-linux-gnu/qt5/QtCore/qlist.h, line 552
-
@Uberlinc said in QString Split() issue.:
"" 1 ("")
looks like you have an empty line in your file.
I would suggest simply skipping, if your don't get the required amount of entries after the splitstrTmp = linest.split(","); if(strTmp.size() < 7){ qDebug() << "Invalid linest:" << linest; return; }
-
@J-Hilk said in QString Split() issue.:
looks like you have an empty line in your file.
I would suggest simply skipping, if your don't get the required amount of entries after the splitI did see that, and so I checked the file.
Yes, there was a blank line at the end of the file.
So, I removed it.
Definitely not there now.The same error occurred.
Weird?
Here is the code to read the file.
Could the error be here instead?void MainWindow::readInfile(QString filename) { QFile file(filename); if(!file.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::about(this, "Error", "Could not open file for reading"); return; } QTextStream stream(&file); QString line = stream.readLine(); while (!line.isNull()) { // process information line = stream.readLine(); loadVec(line); } QMessageBox::about(this, "Done", "File contents read in!"); file.close(); }
-
@Uberlinc said in QString Split() issue.:
Could the error be here instead?
Please first check the string you're splitting!
This is first thing to do in such a situation...
You also should ALWAYS check the indexes when handling data! Not just think it will be exactly what you expect. -
Hey, maybe this will help you somehow:
tmp = line.split(",", QString::SkipEmptyParts); for (int i = 0; i < tmp.count(); ++i) { arrstuff[i] = tmp[i].toInt(); std::cout << arrstuff[i] << " "; }
-
@jsulm said in QString Split() issue.:
Please first check the string you're splitting!
This is first thing to do in such a situation...I did.
Please see the qDebug output above.
It definitely contains commas.
I have removed any and all spaces/carriage returns at the end of the file.
From what I can see, the lines are indeed being read in as per QDebug output above.Thanks.
-
@Uberlinc
line.isNull()
!=line.isEmpty()
also you're reading the line twice and not checking against empty with the 2nd read...
QTextStream stream(&file); while (!stream.atEnd()) { // process information QString line = stream.readLine(); if(!line.isEmpty()) loadVec(line); }
-
@Uberlinc said in QString Split() issue.:
Please see the qDebug output above
Where?
You posted new code but no new debug output as far as I can see.
Please post the string you're trying to split. You expect it to have at least 5 commas, right?
Does the string in question contain 5 commas?