简体   繁体   English

在Qt中读取文本文件

[英]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. 是否存在像badaOS一样的StringTokenizer操作的类。 I have tried QFile but it is not able to read whole file. 我试过QFile但是它无法读取整个文件。

QTextStream lets you read line by line QTextStream允许您逐行阅读

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. 如果它是一个非常大的文件,那么你可以使用file.read(an_appropriate_number)读取,而file.atEnd()是false。

Read a chunk (with file.read()), add it to a temporary string buffer and search for a ',' (eg with QString's contains() method). 读取一个块(使用file.read()),将其添加到临时字符串缓冲区并搜索“,”(例如使用QString的contains()方法)。 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. 如果它包含','则将其拆分(使用QString的split()方法):前X个部分(读取1000个字符可能包含多个1个令牌)将包含找到的令牌,最后一个不是完整的令牌。 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. 因此,将临时字符串切换到拆分的最后一部分并读取另一个块(直到您点击file.atEnd())并将其附加到临时字符串缓冲区。 This will work efficiently unless your tokens are huge. 除非您的代币很大,否则这将有效地工作。 And don't forget to handle the last buffered text after you hit file.atEnd() :) 在点击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). 或者作为替代方案,您可以逐个字符地读取文件并手动检查“,”但是读取超过1个字符总是更好(如果您阅读更多,则效率更高)。

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 我在一个1600行文件上测试了它,它似乎在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. 但是当一个字符串分成两部分时,你将不得不处理这些情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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