0

假设我在一个文件中写入了一些信息,并且它使用n循环写入,例如如下:

a,a,a,a,
b,b,b,b,
c,c,c,c,
a,a,a,a,
b,b,b,b,
c,c,c,c,
.......
a,a,a,a,
b,b,b,b,
c,c,c,c,

现在我想打开文件检查第一行,找到它重复的地方,然后删除所有内容。对于我的示例案例,假设我想在a,a,a,a,再次相遇的地方缠绕,并删除它,以及之后的所有内容,而不是得到以下内容:

a,a,a,a,
b,b,b,b,
c,c,c,c,

问:我该怎么做?

4

2 回答 2

1

You want to remove duplicate lines in a file. If you follow the next steps you will get what you want.

  • Create a vector that will store the hashes of unique lines ( QVector<QString>) Notice that using QMap would be faster.
  • Create an ouput file
  • For every line in the file calculate it's hash. Use QCryptographicHash or qHash (in this case you should have a vector of uints.
    • If the calculated hash is contained in the vector skip this line
    • Otherwise add the hash to the vector and print the line to the output file.
  • At the end the output file should contain only unique instances of the input file.
于 2013-09-03T14:20:50.370 回答
1

您可以使用 QTextStream 流式传输您的文件(因此,不要关心 RAM)。然后使用 readLine() 函数一次读取一行到 QString,并与新行进行比较。这里有一些代码示例:

int lineCounter = 0; //count for line
QFile f(filePath);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
    return false;
QTextStream stream(&f);
QString line;
// read the first line
line = stream.readLine();
lineCounter++;
QString hash = QString(QCryptographicHash::hash(line.toAscii(), QCryptographicHash::Md5).toHex());
do
{
    line = stream.readLine();
    if (QString(QCryptographicHash::hash(line.toAscii(), QCryptographicHash::Md5).toHex()).compare(hash) == 0)
    {
        // Save data from 1st line to "lineCounter" to new file, or do your own works;
        // and then break;
    }
    lineCounter++;
} while (!line.isNull());
于 2013-09-04T14:49:23.240 回答