假设我在文件中写了一些信息,并以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,
问:我怎么能这样做?
答案 0 :(得分:1)
您想要删除文件中的重复行。如果您按照以下步骤操作,您将获得所需的信息。
QVector<QString>
)请注意,使用QMap
会更快。 QCryptographicHash
或qHash
(在这种情况下,您的矢量应为uints
。
答案 1 :(得分: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());