Retrieve Specific Lines from a FileAuthor: Ashley Truter This example will show you how to retrieve specific lines directly from a file. This can be useful when you have large files or memory is at a premium. First, for the examples below, we need to create a sample file with three lines of text:
Ordinarily, we would load this file as lines into memory with:
and then access it like an array:
What we want to do in this example, however, is read lines from the file without having to load it into memory. REBOL lets us access a file directly by way of a port. We can open and close our sample file with the following code:
Using SKIP we can now write a simple seek function:
The magic occurs with
which is just saying, "Print the first value of the remaining values in the file, *after* skipping ahead a number of lines less one (for the line I am interested in)". With just a few more lines of code we can retrieve more than one line at a time with:
which is doing what our first example did but "remembering" where we are in the file. This function can obviously be improved in a number of ways. What happens when a non-integer is provided as input? A number smaller or larger than the number of lines? What happens if the numbers are not provided in ascending order or the provided file name does not exist? |