Solved "index out of range"
-
Hello everyone.
First of all thanks a lot for helping me.I am trying to get information from a .txt file.
I have to get the first four words of each line.This is my code:
if(file.open(QFile::ReadOnly)) { std::cout<<("1)"); ui->textEdit->setPlainText(file.readAll()); //Mostramos el texto en un a ventana std::cout<<("2"); /************************* pinta las líneas *************************/ std::cout<<("Estoy en print_lines)")<< std::endl; QString texto = ui->textEdit->toPlainText(); QStringList lines = texto.split("\n", QString::KeepEmptyParts); //Split the text in lines and save each line in Array called "lines" QStringList words; //Por tanto ya tenemos todas las líneas en un array y podemos acceder a ellas como : linea1=myStringList(1); int num_lines = lines.size(); //number of lines that text has. (it always takes one line more that really it is) QString x_start[num_lines]; QString y_start[num_lines]; QString x_end[num_lines]; QString y_end[num_lines]; QString lines_split[num_lines]; for (int i=0;i<num_lines-1;i++) { lines_split[i] = lines.at(i); words = lines_split[i].split(" ", QString::SkipEmptyParts); //Split each line in words x_start[i] = words.at(0); //Take the first word y_start[i] = words.at(1); //Take the second word x_end[i] = words.at(2); //Take the third word y_end[i] = words.at(3); //Take the fourth word std::cout<<i << " " << x_end[i].toStdString()<< std::endl; } }
}
}It compiles fine but it drops an error that it is the next:
ASSERT failure in QList<T>::at: "index out of range", file ../../../Qt5.6.0/5.6/gcc_64/include/QtCore/qlist.h, line 531
The program has unexpectedly finished.And it enters in for just once...
Could everybody help me?
Thanks a lot.
-
You should debug your code to see what happens.
One note: this line is wrong
for (int i=0;i<num_lines-1;i++) // it should be for (int i=0;i<num_lines;i++) // else you skip last line
-
Hi, welcome to devnet.
You split the original file contents using the
QString::KeepEmptyParts
option. This means that if there are any empty lines they will be stored as well. There's probably also a new line character at the end, that's why you think that it's one item more than there is. It's probably an empty item at the end.The above is the problem, because then you split each line and silently assume there are always at least 4 elements. If the line is empty the split will produce an empty list and referencing its 0,1,2,3 element is the
out of range
error.To correct the code use
QString::SkipEmptyParts
in your initial split of the file contents. This will make sure you've got no empty strings in the list.
Then, after you split each line, make sure it actually has at least 4 elements and append the values to a list instead of using an index:QStringList x_start; QStringList y_start; QStringList x_end; QStringList y_end; for (int i = 0; i < num_lines; ++i) //go all the way, don't skip the last item { words = lines.at(i).split(" ", QString::SkipEmptyParts); if (words.size() >= 4) { x_start << words.at(0); //Take the first word y_start << words.at(1); //Take the second word x_end << words.at(2); //Take the third word y_end << words.at(3); //Take the fourth word std::cout << i << " " << x_end.last().toStdString() << std::endl; } }
-
@Chris-Kawa Thanks a lot for answering!
now it works fine!! -
@jsulm Yes my friend I want to skip last line because .size takes size of lines+1 but I do not know why...