QString Split() issue.
-
@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
-
@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
-
@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; }
-
@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(); }
-
@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] << " "; }
-
@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.@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.
-
@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
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); }
-
@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 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?