![]() |
Jumping to a specific line number in a text file
Hello, a friend has asked me to make a program for him, and in this program i need to read lines and interpret them from a script-type of file (by script-type, it will have text in it with commands that the program will execute, script as in typed script...kind of like what the web browser does with JavaScript.)
What i would like to know is if there is an easy way to just jump to a certain line in a text file, like line 687, for example, without having to start from the beginning and read each line to get to line 687? If there isnt, i was planning on using something like this, but if you know a more efficient way that would be great. :
On a slightly different track, but on the same subject, would it be effective to load the entire text file into a multi-dimensional array, so for line 687 i could just call lines[686] (because lines[0] would be the line number 1)? |
Re: Jumping to a specific line number in a text file
A file with variable length lines has no way to directly address lines other than by counting line endings. Line endings are not magical, they're just another binary code, like the rest of the file.
A file with fixed length lines, via padding, can be address by using fseek (or the appropriate equivalent). Disk I/O is orders of magniture slower than memory access, so your final question depends upon whether you will be accessing the data more than once, or in random order. In the latter case, reading lines into a 2D array would be advisable sometimes, depending upon the probability that you might have to access any line in the file. You're going to have to read all the lines that occur before the latest line you have to deal with (assuming the variable length lines). If there's a significant probability that you're going to need the earlier lines at some point, then you may as well save them in memory and preclude having to do the slow mechanical operations more than once. You might also investigate memory-mapped files. It's situations like this that take you beyond your language and into the implementation of the system as a piece of hardware and as a system governed by some particular OS and the functions thereof. If you're going to encounter this sort of thing very often, digging a little deeper into the actual implementations might be advisable. |
Re: Jumping to a specific line number in a text file
You could load the lines into an array. Whether or not this is a practical solution depends a lot on what you're doing with them, and how big the files are. Presumably, since it's a script, you're wanting to jump from line to line in a random-access fashion to handle logic flow constructs, such as loops and conditionals.
Assuming this is the case, your best bet is to allocate a block of memory big enough to hold the file, and load it into this buffer. You can then iterate through the file, counting the newlines at the end, and determine from this how many lines are in the file (remember that the last line may not end with a newline). Allocate an array of this many size_t elements, and then iterate through the buffer again, filling the size_t array with the line offsets (ie, the buffer positions of the start of each line). Now you can use fseek() to jump to the start of any line you like, without needing to read the file (this is dependent on the file not changing during your program's run, of course). A variant on this, if you're comfortable with the vector template type, is to create a size_t vector, and fill it in as you count lines. This avoids a second pass through the buffer, but has the drawback of potentially wasting an arbitrary amount of memory, depending on what size you pick for your vector.The above method requires you to know how to find the file's size; you can check your OS and compiler for API or library methods to do this. If you can't find any such methods, an alternative is to fseek() to the file's end, and then use ftell() to get the position.The reason to do it this way, rather than allocate a separate block for each line in the file, is to avoid so many calls to new. Each allocation will incur overhead, and if you can minimize the number of allocations, you're better off. Another drawback to repetitive allocations is fragmentation of the heap, but this depends a lot on how you're doing things. |
Re: Jumping to a specific line number in a text file
Thank you two for the speedy replies. I think i will use vectors, i had forgotten about them until you mentioned them, lectricpharaoh. I will also look up "memory-mapped files" on google and see what i can find out about them.
The lines of the file will be variable lengths and the program will need to be able to go to any specific line at any time and read from there on (like loops.) As for my OS and compiler, i am running Windows XP and using GNU GCC Compiler. |
Re: Jumping to a specific line number in a text file
:
It's from an old project of mine. It will have a file as an input and will result in a vector holding the lines. I hope it helps you. |
| All times are GMT -5. The time now is 3:50 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC