Q(uick)BASIC Statement: WRITE #

Quick View

WRITE #

A file I/O statement that writes data to a sequential file

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • WRITE [[#]filenumber%,] expressionlist
Description/Parameter(s)
filenumber% The number of an open sequential file. If the file number is omitted, WRITE writes to the screen.
expressionlist One or more variables or expressions, separated by commas, whose values are written to the screen or file.
WRITE inserts commas between items and quotation marks around strings as they are written. WRITE writes values to a file in a form that can be read by the INPUT statement.
Example
CLS OPEN "LIST" FOR OUTPUT AS #1 DO INPUT " NAME: ", Name$ INPUT " AGE: ", Age$ WRITE #1, Name$, Age$ INPUT "Add another entry"; R$ LOOP WHILE UCASE$(R$) = "Y" CLOSE #1 'Print the file to the screen. OPEN "LIST" FOR INPUT AS #1 CLS PRINT "Entries in file:": PRINT DO WHILE NOT EOF(1) INPUT #1, Rec1$, Rec2$ 'Read entries from file. PRINT Rec1$, Rec2$ 'Print the entries on the screen. LOOP CLOSE #1 KILL "LIST"
Syntax
  • WRITE #filenumber[,expressionlist]
Description/Parameter(s)

The filenumber is the number used in the OPEN statement. The file must be opened in OUTPUT or APPEND mode. The expressions in the argument expressionlist are string and/or numeric expressions, separated by commas. If you omit the expressionlist, the WRITE # statement writes a blank line to the file.

The WRITE # statement, unlike the PRINT # statement, inserts commas between items as they are written to the file. You do not have to put explicit delimiters in the list. A new line is inserted once the last item in the list has been written to the file.

If WRITE # attempts to write data to a sequential file restricted by a LOCK statement, an error message appears that reads "Permission denied" unless the error is trapped by the program. All of BASIC's usual error-handling routines can trap and examine this error.

Example

The output from the following program illustrates the difference between the WRITE # and PRINT # statements:

A$ = "VCR, remote control" : B$ = "$399.00" OPEN "PRICES.DAT" FOR OUTPUT AS #1 'Open PRICES.DAT for writing PRINT #1,A$,B$ 'Store A$ and B$ in first record with PRINT #. WRITE #1,A$,B$ 'Store A$ and B$ in second record with WRITE #. CLOSE #1

Sample Output:

Use the QuickBASIC editor to see what the contents of the file PRICES.DAT looks like: 1. From the File menu, choose the Open Program command. 2. Specify the file name PRICES.DAT in the dialog box. 3. After you are prompted to save the program, the contents of PRICES.DAT will be displayed in the View window: VCR, remote control $399.00 "VCR, remote control","$399.00" 4. To clear the View window, choose the New Program command from the File menu.
Syntax
  • WRITE #filenumber%[,expressionlist]
Description/Parameter(s)
filenumber% The number used in an OPEN statement to open the file that will contain the data being written. The file must be opened in output or append mode.
expressionlist One or more string or numeric expressions separated by commas. If expressionlist is omitted, a blank line is written to the file.

Usage Notes

  • The WRITE # statement, unlike the PRINT # statement, inserts commas between items and quote marks around strings as they are written to the file. You do not have to put explicit delimiters in the list. A new line is inserted once the last item in the list has been written to the file.
  • If you attempt to write data to a sequential file restricted by a LOCK statement, BASIC generates the error message, "Permission denied " appears unless it is trapped by the program. All of BASIC's" usual error-handling routines can trap and examine this error.
Example

This example uses the WRITE # statement to write customer information to a data file. The LINE INPUT # statement is then used to read the records from the file.

OPEN "LIST" FOR OUTPUT AS #1 PRINT "CUSTOMER INFORMATION:" 'Get customer information. DO PRINT INPUT " LAST NAME: ", LName$ INPUT " FIRST NAME: ", FrName$ INPUT " AGE: ", Age$ INPUT " SEX: ", Sex$ Sex$ = UCASE$(Sex$) WRITE #1, LName$, FrName$, Age$, Sex$ INPUT "Add another"; R$ LOOP WHILE UCASE$(R$) = "Y" CLOSE #1 'Echo the file back. OPEN "LIST" FOR INPUT AS #1 CLS PRINT "Records in file:": PRINT DO WHILE NOT EOF(1) LINE INPUT #1, REC$ 'Read records from file. PRINT REC$ 'Print the records on the screen. LOOP 'Remove file from disk. CLOSE #1 KILL "LIST"

Sample Output:

CUSTOMER INFORMATION: LAST NAME: Saintsbury FIRST NAME: Aloysius AGE: 35 SEX: m Add another? y LAST NAME: Frangio FIRST NAME: Louisa AGE: 27 SEX: f Add another? n Records in file: "Saintsbury","Aloysius","35","M" "Frangio","Louisa","27","F"