Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎


Keyword Abbreviation Token (hex) Version(s) Classification
DCLOSE DC{Shift+L} CE 4.x Command and Statement
RLUM R{Shift+L} CE 3.5 Function
DCLOSE DC{Shift+L} FE 0F 7.0 Command and Statement

DCLOSE [ # fileNumver ] [ , driveNumber ] [ { , | ON } U unitNumber ] ... [ , ItwoChar ] [ , R ] ... 
Parameters Type Legal Value(s) Default Value Note(s)
fileNumber Unsigned byte  0 ~ 255  all files open on unitNumber   
driveNumber Integer 0 or 1 0 Non-literal must be enclosed in parentheses ()
unitNumber Integer  8 ~ 11  Non-literal must be enclosed in parentheses () 
twoChar Char[2]  any    Must be two literal characters. 
Close a specific file, or "safe" close all files on a specific device.

DCLOSE without a fileNumber closes all files open on the device given (or implied) by unitNumber.  This is similar to DCLEAR but no "initialize" command is sent to the device.  It is also similar to CLR, but more specific (only affects one device), and more imporatantly DCLOSE really closes the files (CLR just "forgets" them which can result in corrupted data if any files were open for writing).
With a fileNumber, BASIC will close that specific file.  If the file is not open, there will be no error (assuming the Syntax is valid).  Assuming the file was open and has been closed, there is a (potential) bug: the secret variable "DosFA" is not set to the device corresponding to the file.  This is appropriate if the file is not a "disk device" (don't know why you would use DCLOSE in that case!).  Assuming the file was for a "disk" device, then the secret "DosFA" may be set to the wrong value (it is set to 8 unless you give a specific unitNumber).
When you close a file for writing, it is usually a good idea to check the device status with DS or DS$ to be sure it was closed appropriately.  However because of the bug just described, this may report on the wrong device.  A good example would be a system with two "disk" devices, unit 8 and unit 9 (both turned on with and working normally).  If you write a file to unit 9, then DCLOSE it without specifiying unitNumber 9, then when you try to check for errors with DS or DS$, you will get an "OK" message from unit 8.  This would allow an error with your file (on unit 9) to go unnoticed.  Worse, if device 8 is not powered on, you would get a DEVICE NOT PRESENT ERROR when trying to test DS or DS$.
You can avoid the bug if you always give the correct unitNumber corresponding to the file you are closing.  This means you have to be responsible for remembering which fileNumber is using which unitNumber.  Simple task in a simple program (using constant values), more work and potential for error in more complex programs.  Really sad because the system has the information already, but BASIC just throws it away.
If an expression (enclosed in parentheses) is not valid, or an expression is used without parentheses, SYNTAX ERROR occurs.  If any parameter is not the correct type a TYPE MISMATCH ERROR will be generated.  Otherwise if a parameter is not a legal value (see table above), an ILLEGAL QUANTITY ERROR is generated.
Like all disk commands and statements, the Syntax is more flexible than shown above.  In particular, the parameters may be given in any order.  The general restrictions are: a comma (,) must not precede the first parameter (ON may do so), any non-literal value (a variable name or expression) must be enclosed in parentheses (), and do not supply the same parameter more than once.  Exceptions include the U and R parameters, which may used an unlimited number of times (R is ignored and the last unitNumber is used), and the twoChar parameter which must always be two literal characters (it may not be repeated although it is otherwise ignored). 
If the specified fileNumber was open for writing, you may want to check the error/status channel with DS or DS$ to ensure it was properly closed by the device.  This generally only fails if the disk is full.
Like all disk-based commands, DCLOSE restricts the driveNumber to either 0 or 1, but it doesn't matter because the driveNumber is not used by BASIC.
Like all disk-based commands, DCLOSE will reset DS$ and set the secret variable "DosFA" to the unitNumber (defaults to 8 if not given), even if the fileNumber was opened on a different device!  If any files were actually closed, it also indirectly updates ST.
DCLOSE      : REM close all files on unit 8
DCLOSE U9   : REM close all files on unit 9
DCLOSE #7   : REM close fileNumber 7 on any device and set "DosFA" to 8
  Compare With  
  Contrast With  
  See Also  
© H2Obsession, 2014