Chapter
10
I
BASIC
Keywords
LOF
Function
LOF
(
buffer)
Returns the length of the file in bytes.
Buffer
is
the number assigned to the file when you opened it.
Example
Y
=
LOF(5)
assigns the length of the file in bytes to variable
Y
Sample Programs
During direct access to an existing file, you often need a way to
know when you have read the last valid record.
LOF
provides a
way:
1540 OPEN
"R",
1,
"unknown.txt",
128
1550 FIELD 1, 128 AS
A$
1560 RCNUM%
=
1 'START AT BEGINNING OF FILE
1570 RCSIZ%
=
128 'SET RECORD SIZE
1580 IF RCNUM%
*
RCSIZ%
>
LOF(1) GOTO 1640
1590 'CHECK FOR END OF FILE
1600 GET 1, RCNUMX 'RECORD NUM. TO BE ACCESSED
1610 PRINT
AS
1620 RCNUM%
=
RCNUM%
+
1 'INCREMENT RECORD NUM
1630 GOTO 1580
1640 CLOSE
If you attempt to
GET
record numbers beyond the end-of-file,
BASIC gives you an error.
These lines use
LOF
to determine where to start adding when
you want to add to the end of
a
file:
1700 RCNUM%
=
(LOFC1)
/
RCSIZ%)
+
1
1720 'HIGHEST EXISTING RECORD
1720 PUT 1, RCNUM% 'ADD NEXT RECORD
210