View Single Post
Old Oct 30th, 2007, 2:28 AM   #3
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote