Results 1 to 6 of 6

Thread: Readline from a particular index

  1. #1
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Readline from a particular index

    Suppose I have atext file with a large number of lines.
    Every lines can be of different size.
    I have to read only that line wich meet some rules.
    If the line is at the end of the file, I must scan all the line, the operation is slow.
    But suppose that I know the position of the line I have to read, e.g. the 500th.
    Therse is a way with QTextStream or somethinglike to read a particular line of a text file
    without scanning all the file?
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Readline from a particular index

    Is it a regular text file? Then it's not possible to know where line 500 is without parsing the previous lines for CRLF characters or other control characters that seperate lines

    My suggestion: use a database or an xml file if possible

  3. #3
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Readline from a particular index

    Yes it is. Every line has CRLF.
    By the way, the databse version of the file would be much bigger than the oringal file. Therefore I asked that question.
    G

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Readline from a particular index

    Well I also know only the possibilit to loop till you reach your line. One other possibility could be to create an index file to your original file, storing the beginning of each line. Then you could use QFile::seek(). It's not nice but could fasten up your querries.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Readline from a particular index

    Writes would be nasty though, you would have to rebuild the index every time, but as you say, it's the only real way of doing such a task.

    A database may be larger, but would do all the indexing for you. Why write your own database handling routines?

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Readline from a particular index

    Given the problem statement, you have no choice but to do a linear search.

    It might be possible, though, to speed this up if the machine you're using is up to the task. Split the file into several segments, and feed each segment to a separate thread. The first thread to return wins. This will cut your search time, on average, by the number of threads, IF each thread gets it's own core to run on.

    It might be worth taking a look at Qt's new concurrency classes. Or something like OpenMP.

    Unless there is some additional order imposed by the rules you're searching on, or some other factor that hasn't been noted, it's all going to boil down to rummaging through the file one line at a time until you find a match.

Similar Threads

  1. QBuffer::readLine()
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 23rd April 2010, 21:57
  2. readLine in QProcess
    By grisson in forum Qt Programming
    Replies: 7
    Last Post: 15th October 2009, 18:39
  3. Qtextstream::readline()
    By dawn_to_dusk_ in forum Qt Programming
    Replies: 4
    Last Post: 3rd August 2008, 18:58
  4. readLine() terminates with the '.' character
    By gQt in forum Qt Programming
    Replies: 5
    Last Post: 30th May 2008, 12:31
  5. readLine problem
    By gQt in forum Qt Programming
    Replies: 10
    Last Post: 29th February 2008, 10:35

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.