Q(uick)BASIC Statement: CLOSE

Quick View

CLOSE

A file I/O statement that concludes I/O to a file or device

Worth knowing

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

Syntax
  • CLOSE [[#]filenumber%[,[#]filenumber%]…]
Description/Parameter(s)
filenumber% The number of an open file or device.
  • CLOSE with no arguments closes all open files and devices.
Example
CLS INPUT "Enter filename: ", n$ OPEN n$ FOR OUTPUT AS #1 PRINT #1, "This is saved to the file." CLOSE OPEN n$ FOR INPUT AS #1 INPUT #1, a$ PRINT "Read from file: "; a$ CLOSE
Syntax
  • CLOSE [[#]filenumber[,[#] filenumber]…]
Description/Parameter(s)

The filenumber is the number under which the file was opened. A CLOSE statement with no arguments closes all open files and devices.

The association of a file with a file number ends when a CLOSE statement is executed. You may then reopen the file using the same or a different file number. Once you close a file, you may use that file's number for any unopened file.

A CLOSE for a file or device that was opened for sequential output writes the final buffer of output to that file or device.

CLOSE releases all buffer space associated with the closed file or files.

The CLEAR, END, RESET, RUN, and SYSTEM statements close all files automatically.

Example

The CALL statement programming example illustrates a use of the CLOSE statement.

Syntax
  • CLOSE [[#]filenumber%[,[#]filenumber%]...]
Description/Parameter(s)
filenumber% The association of a file, device, or ISAM table with the argument filenumber% ends when a CLOSE statement is executed. You then can reopen the file, device, or table using the same or a different file number.

New Features

  • CLOSE commits any pending ISAM transactions.

Usage Notes

  • CLOSE with no arguments closes all open tables and files.
  • A CLOSE for a file or device that was opened for sequential output writes the final buffer of output to that file or device.
  • CLOSE releases all buffer space associated with the closed file, device, or table.
  • The CLEAR, END, RESET, RUN, and SYSTEM statements, and the CHAIN statement when used with the /O command line option, also close all files, devices, and tables.

Programming Tips

  • Any ISAM operation that closes a table causes transactions to be committed. For example, if a type mismatch occurs while you are opening an ISAM table, the table is closed and a pending transaction is committed.
  • You may wish to code your programs so they first open all tables, then perform all transactions, then close tables. Make sure any operation that can close a table occurs outside a transaction.

Example

This example uses FREEFILE to obtain the next available file number. It uses the OPEN statement to open a sequential file, the CLOSE statement to close the file, and the KILL statement to delete the file from the disk.

DIM Filenum AS INTEGER CLS INPUT "Enter file name: ", filename$ Filenum% = FREEFILE OPEN filename$ FOR OUTPUT AS Filenum% PRINT : PRINT UCASE$(filename$); " opened for output as File #"; Filenum% 'Put something in the file. PRINT #Filenum%, "The quick brown fox jumped over the lazy yellow dog." 'Close the file. CLOSE Filenum% Filenum% = FREEFILE OPEN filename$ FOR INPUT AS Filenum% PRINT : PRINT UCASE$(filename$); " has been reopened for input." LINE INPUT #Filenum%, L$ PRINT : PRINT "The contents of the file are:"; L$ CLOSE Filenum% 'Remove the file from disk. KILL filename$