Search code examples
c++qtqfileqvector

Qt reading file and mapping to QVector very slow (crashes)


So what I am trying to do is read a file and map it to a two dimensional QVector. Here is my code so far

void dataModel::parseFileByLines()
{
QVector<QVector<QString> > dataSet;
lastError = "";
QRegExp reg(fileDelimiter);
QFile inFile(inputFile);
if (inFile.open(QIODevice::ReadOnly)){
    QTextStream fread(&inFile);
    long totalSize = inFile.size();
    QString line;
    while(!fread.atEnd()){
        line = fread.readLine();  
        dataSet.append(line.split(reg,QString::KeepEmptyParts).toVector());
   }
}else{
   lastError = "Could not open "+inputFile+" for reading";
}
}

My issue is that when dealing with 1000,000 lines or more the program crashes with a message saying "This application has requested the Runtime to terminate it in an unusual way". Is there a more efficient way I can achieve my goal ? If so how ?

The input file may be in a format like so

ID,NAME,AGE,GENDER...etc

1,Sam,12

...

...

1000000

I would really appreciate any help or advice


Solution

  • I have tested this (QList) version here on my computer and it runs much faster then the QVector version and I also believe it will not crash.

    void parseFileByLines(QString inputFile)
    {
        QList<QList<QString> > dataSet;
        QString lastError = "";
        QFile inFile(inputFile);
        if (inFile.open(QIODevice::ReadOnly)){
            QTextStream fread(&inFile);
            long totalSize = inFile.size();
            QString line;
            while(!fread.atEnd()){
                line = fread.readLine();
                QList<QString> record = line.split('\t',QString::KeepEmptyParts);
                dataSet.append(record);
            }
        }else{
            lastError = "Could not open "+inputFile+" for reading";
        }
    }