BASIC · lesson 19 of 30 · Files and errors · bonusWrite and read a file
open takes "w" to write, "a" to add to the end and "r" to read. eof tells you the file has ended.
19-files.yab Download 19-files.yab
rem "w" makes a new file, "r" reads it, "a" adds to its end
open #1, "shopping.txt", "w"
print #1, "two disks"
print #1, "a network cable"
close #1
open #1, "shopping.txt", "a"
print #1, "a UPS"
close #1
open #1, "shopping.txt", "r"
n = 0
while not eof(1)
line input #1 line$
n = n + 1
print n, ". ", line$
wend
close #1
Terminal
$ yabasic 19-files.yab
1. two disks
2. a network cable
3. a UPS
$ cat shopping.txt
two disks
a network cable
a UPSExercise
Write ten numbers into a file and read back their sum.