0

Let's say I write some information in a file, and it writes with n cycles, for example as follows:

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,

Now I want to open the file check the first line, find where it repeats and delete everything after that. For my example case let's say I want to wind where a,a,a,a, meets again, and to delete it, and everything after that, getting the follows instead:

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

Q: How can I do that?

Mike
  • 563
  • 5
  • 15
  • 32

2 Answers2

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.
pnezis
  • 12,023
  • 2
  • 38
  • 38
  • Hi, thanks for the answer. That does not actually fit me, as the number of cycles can be very big, and I do not want to keep so much information in RAM. I want to do everything directly in the file. – Mike Sep 03 '13 at 14:24
  • @Mike create a temp file. It's not possible to insert or delete in a middle of a file. Not sure if you want this, but sounds like it. – hyde Sep 03 '13 at 14:41
1

You can use QTextStream to stream your file (so, do not care about RAM). Then use readLine() function to read one line at a time to QString, and compare with new line. Here some code sample:

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());
William Truong
  • 237
  • 3
  • 12