Home Previous section Next section

Reading and writing to files


To save data to a file:
  1. Call the OPEN command, specifying the file name, file mode (OUTPUT), and file number.

  2. Use PRINT, followed by the file number and the data you want to write.

  3. Close the file using the CLOSE command.


The following opens a file, using mode OUTPUT and number 1, and then saves the text Hello World! to the file:
    OPEN "testfile.dat" FOR OUTPUT AS #1
    PRINT #1, "Hello World!"
    CLOSE #1

To open a file for "reading," call OPEN and pass INPUT as the file mode. Then you can read the data by using the INPUT command.
    OPEN "testfile.dat" FOR INPUT AS #1
    INPUT #1, text$
    CLOSE #1

    PRINT text$
Output:
    Hello World!

Next section


Feel free to distribute this tutorial, upload it to your website, link to it from your site, etc.

http://www.geocities.com/progsharehouse/qbtutor
Mirror: http://development.freeservers.com/qbtutor