OPEN fname$ FOR mode AS [#]fnbr
Opens a file for reading or writing.
‘fname’ is the file name (8 chars max) with an optional extension (3 chars max) separated by a dot (.). It can be prefixed with a directory path.
For example:
OPEN "B:\DIR1\DIR2\FILE.EXT" FOR INPUT AS #1
‘mode’ is INPUT or OUTPUT or APPEND.
INPUT will open the file for reading and throw an error if the file does not exist.
OUTPUT will open the file for writing and will automatically overwrite any existing file with the same name.
APPEND will also open the file for writing but it will not overwrite an existing file; instead any writes will be appended to the end of the file. If there is no existing file the APPEND mode will act the same as the OUTPUT mode (i.e. the file is created then opened for writing).
Note: APPEND is not supported on the flash file system (drive A:).
‘fnbr’ is the file number (1 to 10). The # is optional. Up to 10 files can be open simultaneously. The INPUT, LINE INPUT, PRINT, WRITE and CLOSE commands as well as the EOF() and
INPUT$() functions all use ‘fnbr’ to identify the file being operated on.
See also OPTION ERROR and MM.ERRNO for error handling.
The length of the path plus the file name (including punctuation) must be less than 127 characters.
The following code will try to open a file. If it is not there, an error occurs.
1 2 |
<span style="color: #008000;">' Stop error from halting program</span><span style="color: #008000;"> OPTION ERROR CONTINUE</span> |
1 |
<span style="color: #008000;">FileStatus = 1</span> |
1 |
<span style="color: #008000;">OPEN "test.txt" FOR INPUT AS #1</span> |
1 2 |
<span style="color: #008000;"> ' Check error condition</span> <span style="color: #008000;"> IF MM.ERRNO = 6 THEN FileStatus = 0</span> |
1 |
<span style="color: #008000;">OPTION ERROR ABORT</span> |
1 2 |
<span style="color: #008000;">' If file opened without error...</span> <span style="color: #008000;">IF FileStatus = 1 THEN</span> |
1 |
<span style="color: #008000;"> ' Process opened file here</span> |
1 |
<span style="color: #008000;">ENDIF</span> |
See also:
CLOSE
EOF
INPUT#
INPUT$
LINE INPUT
PRINT
SEEK
WRITE
Leave a Reply