One solution that presents itself it is to create an array that stores the offset in the file to each line (updated every time you read a line) from the file; that way you can seek almost instantly to any line in the file by looking up its start offset in the array. The downside is that this take up rather large amount of RAM!
You can always work programmatically, working backwards from the current cursor position looking for a CR (13). Supposing you had a file
TEXT.VAR containing the following:
Code:
LINE 1
LINE 2
LINE 3
LINE 4
LINE 5
Then the following program:
Code:
10 h=OPENIN"TEST.VAR"
20 INPUT#h,line$:PRINT line$
30 INPUT#h,line$:PRINT line$
40 PROC_PrevLine(h):PROC_PrevLine(h)
50 INPUT#h,line$:PRINT line$
60 INPUT#h,line$:PRINT line$
70 INPUT#h,line$:PRINT line$
80 PROC_PrevLine(h):PROC_PrevLine(h)
90 INPUT#h,line$:PRINT line$
100 CLOSE#h
110 END
120 :
130 DEF FN_BPEEK#(h)LOCALB%:B%=BGET#h:PTR#h=PTR#h-1:=B%
140 DEF PROC_PrevLine(h)
150 PTR#h=PTR#h-1
160 REPEAT:IF PTR#h>0 THEN PTR#h=PTR#h-1
170 UNTIL PTR#h=0 OR FN_BPEEK#(h)=13
180 IF PTR#h>0 THEN PTR#h=PTR#h+1
190 ENDPROC
would output
Code:
LINE 1
LINE 2
LINE 1
LINE 2
LINE 3
LINE 2
After each
INPUT# the file pointer is pointing at the next line, so you need to call
PROC_PrevLine twice to move the pointer to the previous line.