Text Files - An Overview - ☆
A BASIC program manages file input or output using virtual channels, which are connected with actual files.
The process necessary to use a file is
1) to assign the file to a channel (to say, open the file),
2) to do input or output,
3) to release the assignment of the file to the channel (to say, close the file).
Channels are identified with positive integers.
The channel designated by an integer n is denoted by #n.
Example 1. File Output.
If the file already exists, all the contents of it is erased before the output.
This program outputs also commas so that a program example 2 can read the file.
LET F$ = "A:ABC.TXT" ! Assign the name of a file to a string variable F$. OPEN #1: NAME F$ ! Assign the file "A:ABC.TXT" to a channel #1. ERASE #1 ! Erase the file contents if any. FOR x=0 TO 10 PRINT #1 : x; ","; SQR(x) ! write to the channel #1. NEXT x CLOSE #1 ! Release the assignment to the channel END
A program that reads data from the file written by the above program.
LET F$="A:ABC.TXT" OPEN #2: NAME F$ FOR i=1 TO 10 INPUT #2: a, b PRINT a, b NEXT i CLOSE #2 END
A program that reads every line of a file.
LET F$="A:ABC.TXT" OPEN #2: NAME F$ DO LINE INPUT #2, IF MISSING THEN EXIT DO: a$ PRINT a$ LOOP CLOSE #2 ENDA LINE-INPUT statement read a line from the channel and assign it to the string variable.