Beginner's Programming Tutorial in QBasic

(Kiana) #1

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 #1PRINT #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 #1INPUT #1, text$
CLOSE #1
PRINT text$
Output:
Hello World!

Free download pdf