Page 277 - Learn to Program - Basic-256
P. 277

Chapter 18: Files – Storing Information For Later.                              Page 261


            Chapter 18: Files – Storing Information For

                   Later.





                   We have explored the computer's short term memory with variables and
                   arrays but how do we store those values for later?  There are many different
                   techniques for long term data storage.


                   BASIC-256 supports writing and reading information from files on your hard
                   disk. That process of input and output is often written as I/O.

                   This chapter will show you how to read values from a file and then write
                   them for long term storage.




            Reading Lines From a File:


                   Our first program using files is going to show you many of the statements
                   and constants you will need to use to manipulate file data. There are several
                   new statements and functions in this program.



                1       # readlfile.kbs
                2       # read a simple text file
                3
                4       inputstring "file name> ", fn
                5       if not exists(fn) then
                6          print fn + " does not exist."
                7          end
                8       end if
                9       #
                10      n = 0
                11      open fn
                12      while not eof
                13         line = readline
                14         n = n + 1


                                                           © 2019 James M. Reneau (CC BY-NC-SA 3.0 US)
   272   273   274   275   276   277   278   279   280   281   282