readln() for a certain line
Moderator: Paul Tuersley
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.
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:
Peter
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();
}
}
Thanks Peter, that worked like a charm.