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

by scott, Saturday, October 01, 2011, 00:33 (2213 days ago)

is it possible to read/write numbers from/to a file?
if so, please show me a few lines to do so.
thus far, i've been able to:
- input/print numbers & text from/to the text output area of basic
- read/write strings to a file
but not read/write numbers from/to a file.

read/write numbers from/to a file

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

# 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
print "file read total = " + total

# 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
print "file written"

read/write numbers from/to a file

by scott, Thursday, October 13, 2011, 01:59 (2201 days ago) @ Jim

thanx jim.
i would have answered sooner, but further reading convinced me of that answer, & i wrote a similar work-around, so i hadn't been back til now to see if i owed a kind soul a thanx.

RSS Feed of thread
powered by my little forum