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

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




            Writing Lines to a File:


                   In Program 112 we saw how to read lines from a file. The next two programs
                   show different variations of how to write information to a file. In Program 113
                   we open and clear any data that may have been in the file to add our new
                   lines and in Program 114 we append our new lines to the end (saving the
                   previous data).




                1       # resetwrite.kbs
                2       # write text to a file, go back to begining
                3       # and display the text
                4
                5       open "resetwrite.dat"
                6
                7       print "enter a blank line to close file"
                8
                9       # clear file (reset) and start over
                10      reset
                11      while true
                12         input ">", l
                13         if l = "" then exit while
                14         writeline l
                15      end while
                16
                17      # go the the start and display contents
                18      seek 0
                19      k = 0
                20      while not eof()
                21         k = k + 1
                22         print k + " " + readline()
                23      end while
                24
                25      close
                26      end

            Program 113: Clear File and Write Lines



                                                           © 2019 James M. Reneau (CC BY-NC-SA 3.0 US)
   276   277   278   279   280   281   282   283   284   285   286