PDA

View Full Version : How to know how many lines a text files has?



Momergil
14th July 2013, 17:31
Hello!

Well, the question is in the title. By now I do that with the following function:



int MReadWrite::getLineNumber()
{
if (!isOpen() || !mFile.isReadable() || !mFile.seek(0))
return -1;

uint numboflines = 0;

while (!mFile.atEnd())
{
numboflines++;
mFile.readLine();
}

if (!mFile.seek(0))
qDebug() << "Failure while trying to put streamer at beginning in getLineNumber().";

return numboflines;
}


But the problem is that I want to avoid the usage of the readLine() function, since reading from the hard disk is quite a slow operation and the content is not being actually used anyway.

Is there another, faster way of doing this?

Thanks,

Momergil


(Btw, in case of the above implementation, would it be any better to use a QTextStream to do the readLine()?)

ChrisW67
14th July 2013, 21:45
Read the file as binary in largish blocks (or all at once if you know it is small enough) and count the number of newline characters '\n' in each block: QIODevice::read() and QByteArray::count(). Perhaps add one if the last character is not a new line.

Momergil
15th July 2013, 04:45
Read the file as binary in largish blocks (or all at once if you know it is small enough) and count the number of newline characters '\n' in each block: QIODevice::read() and QByteArray::count(). Perhaps add one if the last character is not a new line.

Hmm, interesting method. But the usage of the count() function is not itself quite a memory eater? I mean, doesn't it goes for each character in the largish block buffer and does a "if (char == '\n')" ? How could I know if I'm actually having a memory usage and time comsumption improvement? (I imagine I didn't mention time comsumption as a quality parameter before, but well add it to the list :) )

---
Btw, is a there way by which one could test if a given implementation of a function is faster than another? Maybe using the OS's API to read the clock in the beginning of the function and at it's end and then comparing to see which took the greatest time (a bad example, I might add, since this difference could easely vary depending on the other tasks/threads the processor took into acount while running the function in both usages).