read/write numbers from/to a file (Language Help)

by Jim ⌂ @, Russell, KY, Monday, October 03, 2011, 01:02 (2211 days ago) @ scott

Here is a sample program. BASIC256 only will read and write string values to a file, so convert the number to strings and convert them back.

# read and write a file of numbers
# j.m.reneau 2011-10-02

filename$ = "numberfile.txt"

top: print "1 to write, 2 to read, 0 to quit"
input num
if num = 0 then end
if num = 1 then gosub writestuff
if num = 2 then gosub readstuff
goto top

readstuff:
# read 10 numbers to a file and show total
open filename$
total = 0
for t = 1 to 10
sn$ = read # read in the next word as a string
n = int(sn$) # convert the string of digits to an integer value
print n
total = total + n
next t
close
print "file read total = " + total
return

writestuff:
# write 10 random numbers to a file
open filename$
reset # go to beginning of file and remove old data
for t = 1 to 10
n = int(rand*100) # random number from 0-99
write string(n) + " " # convert number to a string and write it out
# put space after number to separate it from the next one
next t
close
print "file written"
return


Complete thread:

 RSS Feed of thread

powered by my little forum