简体   繁体   中英

Reading text file in Qt

I want to read a huge text file in which I will be dividing the strings according to the comma (,) and store the strings in the array. So how to do this. Is there any class which does the action as StringTokenizer as in badaOS. I have tried QFile but it is not able to read whole file.

QTextStream lets you read line by line

QFile file(hugeFile);
QStringList strings;
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
    QTextStream in(&file);
    while (!in.atEnd()) {
        strings += in.readLine().split(";"); 
    }
}

You can use file streams.

QFile file = new QFile(hugeFile);      
file.open(QIODevice.OpenModeFlag.ReadOnly);       
QDataStream inputStream = new QDataStream(file);
QStringList array;
QString temp;

while(!inputStream.atEnd()) {
  inputStream >> temp;
  array << temp.split(";");
}

Note that this is untested (pseudo) code, hope it helps.

If it's a really huge file then you can read with the file.read(an_appropriate_number) while file.atEnd() is false.

Read a chunk (with file.read()), add it to a temporary string buffer and search for a ',' (eg with QString's contains() method). If it contains a ',' then split it (with QString's split() method): the first X parts (the read 1000 characters may contain more than 1 tokens) will contain the found tokens and the last one is not a complete token yet. So switch the temporary string to the last part of the split and read another chunk (until you hit file.atEnd()) and append it to the temporary string buffer. This will work efficiently unless your tokens are huge. And don't forget to handle the last buffered text after you hit file.atEnd() :)

Or as an alternative you can read the file character-by-character and check for ',' manually, but it's always better to read more than 1 character (it's more efficient if you read more).

This won't capture whitespace after a comma. If that's not acceptable, feel free to optimize the regex. You can probably also reduce the amount of includes at the top. I was just being thorough. I tested this on a 1600 line file, and it seemed to handle it well in Qt 5.6

#include <QCoreApplication>
#include <QFile>
#include <QIODevice>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QRegularExpressionMatchIterator>
#include <QString>
#include <QStringList>
#include <QTextStream>

int main(int argc, char * argv[])
{
    QCoreApplication app(argc, argv);

    QFile file("C:\\PathToFile\\bigFile.fileExt");
    QStringList lines;
    QStringList matches;
    QString match;

    file.open(QIODevice::ReadOnly | QIODevice::Text);
    while(!file.atEnd())
    {
      lines << file.readLine();
    }
    file.close();

    QRegularExpression regex("(^|\\s|,)\\K\\w.*?(?=(,|$))");
    QRegularExpressionMatchIterator it;

    foreach (QString element, lines)
    {
        it = regex.globalMatch(element);

        while(it.hasNext())
        {
            QRegularExpressionMatch qre_match = it.next();
            match = qre_match.captured(0);
            matches << match;
        }
    }

    return 0;
}

You can always read a part of file:

QFile file( ... );
file.read(1000); // reads no more than 1000 bytes

Or you car read Your file line by line:

file.readLine();

but You'll have to handle cases when one string was splitted in two pieces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM