Results 1 to 3 of 3

Thread: How to know how many lines a text files has?

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to know how many lines a text files has?

    Hello!

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

    Qt Code:
    1. int MReadWrite::getLineNumber()
    2. {
    3. if (!isOpen() || !mFile.isReadable() || !mFile.seek(0))
    4. return -1;
    5.  
    6. uint numboflines = 0;
    7.  
    8. while (!mFile.atEnd())
    9. {
    10. numboflines++;
    11. mFile.readLine();
    12. }
    13.  
    14. if (!mFile.seek(0))
    15. qDebug() << "Failure while trying to put streamer at beginning in getLineNumber().";
    16.  
    17. return numboflines;
    18. }
    To copy to clipboard, switch view to plain text mode 

    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()?)

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to know how many lines a text files has?

    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.

  3. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to know how many lines a text files has?

    Quote Originally Posted by ChrisW67 View Post
    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).

Similar Threads

  1. Replies: 2
    Last Post: 29th May 2013, 20:08
  2. QPlainText Edit and text lines
    By aarelovich in forum Qt Programming
    Replies: 4
    Last Post: 23rd June 2010, 13:09
  3. plot with hor. lines and text
    By hugo vanwoerkom in forum Qwt
    Replies: 2
    Last Post: 10th March 2010, 17:00
  4. I want to get lines of text
    By newplayer in forum Qt Programming
    Replies: 11
    Last Post: 29th July 2008, 09:03
  5. Replies: 4
    Last Post: 25th May 2008, 20:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.