开发者

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. S开发者_高级运维o 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.


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.


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 ',' (e.g. 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜