readln() for a certain line

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

When using the readln() method, how can I have it skip to a certain line? I don't want to have to readln() a bunch of extra lines if I know I need to read lines 11 -123 for example.
vidpat
Posts: 86
Joined: October 21st, 2004, 12:36 am
Location: Phoenix, AZ
Contact:

If all of the lines have have the same number of bytes or if line 11 (per the example) is always n many bytes from the start of the file, you could use seek(). Otherwise, readln() seems like the best approach. A little function goToLine() could be helpful:

Code: Select all

function goToLine(fileF /* open File object */,
        lineI /* line number to go to */,
        relativeB /* boolean to start from current position */) {
    // If we don't want to skip lines relative to our
    //  current position in the file, reset the position
    //  to the beginning of the file.
    if (!relativeB) { fileF.seek(0, 0); }
    // Read through lineI - 1 lines to position at the
    //  start of the desired line.
    for (var i = 0, i < lineI, i++) {
        fileF.readln();
    }
}
Peter
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

Thanks Peter, that worked like a charm.
Post Reply