Ah heck... I decided to post the code instead. Here's my text file viewer using random access files. Compile it and give it a file to read (i.e. myprogram.exe myfile.txt). Throw the largest text file you can at it. It won't even flinch.
'Readme.bas - a text viewer using a random access file as a temp
'holder of text file. This is just a project I did just to say I
'had done it. I did. Yes, there's room for improvement, but I
'never did add to it.
'
'Written by CGFiend
'
'
'strip is a sub to remove chr$(12) (line feed char) from a string
DECLARE SUB strip (a$)
'template for data file
TYPE DataFileTemplate
LineOfText AS STRING * 80
END TYPE
DIM DataFile AS DataFileTemplate
CLS
IF COMMAND$ = "" THEN
PRINT : PRINT "No filename specified. Usage: README filename.ext": PRINT : PRINT
GOTO endit2
END IF
'File not found? etc.
ON ERROR GOTO preend
'record = 1 - record counter
Record = 1
'open input text file
OPEN COMMAND$ FOR INPUT AS #1
'open output temp random access file
OPEN "87654321.TMP" FOR RANDOM ACCESS WRITE AS 2 LEN = LEN(DataFile)
'On slow slow computers this'll display, otherwise it'll be too fast
LOCATE 1, 1: PRINT "Processing..."
'a return point
redo:
'get me a line from the text file
LINE INPUT #1, a$
'check for end of file
IF EOF(1) THEN CLOSE 1: CLOSE 2: GOTO nexter
'strip off chr$(12) (line feed) from string
strip (a$)
'truncate to 80 characters and store in temp data file template
DataFile.LineOfText = LEFT$(a$, 80)
'put line in temp data file
PUT 2, Record, DataFile
'increment record counter
Record = Record + 1
'loop
GOTO redo
'display routine
nexter:
'open temp data file
OPEN "87654321.TMP" FOR RANDOM ACCESS READ AS 1 LEN = LEN(DataFile)
'get max lines in file (max records)
Total = LOF(1) / LEN(DataFile)
'clear screen
CLS
'go to line 2, character 1
LOCATE 2, 1
'display first 20 lines by reading from temp data file
FOR t = 1 TO 20
GET 1, t, DataFile
PRINT DataFile.LineOfText
NEXT t
'set min/max lines displayed on screen
Minimum = 1
Maximum = 20
'Display program title and usage keys on line 22
COLOR 15, 7: LOCATE 1, 1: PRINT " "
LOCATE 1, 1: PRINT COMMAND$
LOCATE 22, 1: COLOR 9, 7: PRINT "CGFiend's Text Engine v1.0"; : COLOR 15, 7: PRINT " Up,Down,PgUp,PgDn,Home,End,Q=Quit ": COLOR 7, 0
'another return point, this one is for user input
redo2:
'trap for a key, if = nothing goto byp
k$ = INKEY$: IF k$ = "" THEN GOTO byp
'if not a scroll key (2 character input) go to byp: and check for
'single character input (Q = Quit)
IF LEN(k$) = 1 THEN GOTO byp
'up
IF ASC(MID$(k$, 2, LEN(k$))) = 72 THEN
IF Minimum - 1 <= 0 THEN Minimum = 1 ELSE Minimum = Minimum - 1: Maximum = Maximum - 1
LOCATE 2, 1: FOR t = Minimum TO Maximum: GET 1, t, DataFile: PRINT DataFile.LineOfText: NEXT t
END IF
'down
IF ASC(MID$(k$, 2, LEN(k$))) = 80 THEN
IF Maximum + 1 > Total THEN Maximum = Total ELSE Maximum = Maximum + 1: Minimum = Minimum + 1
LOCATE 2, 1: FOR t = Minimum TO Maximum: GET 1, t, DataFile: PRINT DataFile.LineOfText: NEXT t
END IF
'left
IF ASC(MID$(k$, 2, LEN(k$))) = 73 THEN
IF Minimum - 20 <= 0 THEN Minimum = 1: Maximum = 20 ELSE Minimum = Minimum - 20: Maximum = Maximum - 20
LOCATE 2, 1: FOR t = Minimum TO Maximum: GET 1, t, DataFile: PRINT DataFile.LineOfText: NEXT t
END IF
'right
IF ASC(MID$(k$, 2, LEN(k$))) = 81 THEN
IF Total < 20 THEN GOTO skipper
IF Maximum + 20 >= Total THEN Maximum = Total: Minimum = Total - 19 ELSE Maximum = Maximum + 20: Minimum = Minimum + 20
LOCATE 2, 1: FOR t = Minimum TO Maximum: GET 1, t, DataFile: PRINT DataFile.LineOfText: NEXT t
skipper:
END IF
'home
IF ASC(MID$(k$, 2, LEN(k$))) = 71 THEN
Minimum = 1: Maximum = 20
LOCATE 2, 1: FOR t = Minimum TO Maximum: GET 1, t, DataFile: PRINT DataFile.LineOfText: NEXT t
END IF
'end
IF ASC(MID$(k$, 2, LEN(k$))) = 79 THEN
Maximum = Total: Minimum = Total - 19
LOCATE 2, 1: FOR t = Minimum TO Maximum: GET 1, t, DataFile: PRINT DataFile.LineOfText: NEXT t
END IF
'a label for skipping scroll key checks if len(K$) = 1
byp:
'exit to DOS if Q pressed
IF UCASE$(k$) = "Q" THEN CLS : PRINT "Text Engine v1.0": PRINT "(c) 1995 CGFiend": PRINT : GOTO endit
'status bar on line 1
'display # of last line on screen
'calculate location in file you are viewing as percent
perc = INT((Maximum * 100) / Total)
LOCATE 1, 20: COLOR 14, 7: PRINT "Line: "; Maximum; " "
LOCATE 1, 40: COLOR 8, 7: PRINT perc; "% ": COLOR 7, 0
'go back and check for keys
GOTO redo2
'error occurred, assume file not found
preend:
PRINT : PRINT "Path\Filename.Ext not found.": PRINT : PRINT
'skip kill of temp file since no temp file created
RESUME endit2
'close and kill temp file
endit:
CLOSE 1
KILL "87654321.TMP"
'end
endit2:
END
'sub to strip chr$(12) (linefeed)
SUB strip (a$)
k$ = a$
IF INSTR(k$, CHR$(12)) THEN
a$ = CHR$(32) + MID$(k$, 2, LEN(k$))
ELSE k$ = ""
END IF
IF a$ = "" THEN a$ = STRING$(80, 32)
END SUB