Solved ASSERT failure in QVector<T>::operator[]: "index out of range" Error
-
Hi Everyone,
I want to count numer of lines in a file.
QFile file("D:/test.txt"); int line_count=0; file.open(QIODevice::ReadOnly); //| QIODevice::Text) QVector<QString> line; QTextStream in(&file); while( !in.atEnd()) { line[line_count]=in.readLine(); line_count++; }
and I get the error " ASSERT failure in QVector<T>::operator[]: "index out of range"
I know I have to add the size here QVector<QString> line (15000) ;
but I am not sure if the file contains more than 15000 for example.
Can anyone help me please :)
-
Hi
Its the
line[line_count]as line is
QVector<QString> line;
and it don't preallocate any entries beforehand.
(ah, you are aware of this. missed that)so you want to do https://doc.qt.io/qt-5/qvector.html#append
line.append( readLine() );
-
Hi
Its the
line[line_count]as line is
QVector<QString> line;
and it don't preallocate any entries beforehand.
(ah, you are aware of this. missed that)so you want to do https://doc.qt.io/qt-5/qvector.html#append
line.append( readLine() );