PDA

View Full Version : Readline from a particular index



giusepped
30th April 2010, 07:47
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

tbscope
30th April 2010, 07:55
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

giusepped
30th April 2010, 08:43
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

Lykurg
30th April 2010, 10:08
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.

squidge
30th April 2010, 18:35
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?

SixDegrees
30th April 2010, 20:58
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.