25.133. OPEN file_name FOR mode AS [ # ] i [ LEN=record_length ]
Open or create and open a file. The syntax of the line is
OPEN file_name FOR mode AS [ # ] i [ LEN=record_length ]
- file_name if the name of the file to be opened. If the mode allows the file to be written
the file is created if it did not existed before. If needed, directory is created for the file.
- mode is the mode the file is opened. It can be:
- input open the file for reading. In this mode the file is opened in read only mode and can not be altered using the file number associated with the open file. Using any function or command that tries to write the file will result in error. In this mode the file has to exist already to open successfully. If the file to be opened for input does not exist the command OPEN raises an error.
- output open the file for writing. If the file existed it's content is deleted first and a freshly opened empty file is ready to accept commands and functions to write into the file. When a file is opened this way no function or command trying to read from the file can be used using the file number associated with the file. The file is opened in ASCII mode but the handling mode can be changed to binary any time.
- append open a possibly existing file and write after the current content. The same conditions apply as in the mode output, thus you can not read the file, only write. The file is opened in ASCII mode but the handling mode can be changed to binary any time.
- random open the file for reading and writing (textual mode). When you open a file using this mode the file can be written and the content of the existing file can be read. The file pointer can be moved back and forth any time using the command SEEK and thus quite complex file handling functions can be implemented. If the file did not exist it is created.
- binary open the file for reading and writing (binary mode). This mode is the same as random with the exception that the file is opened in binary mode.
- socket open a socket. In this case the file name is NOT a file name, but rather an Internet machine name and a port separated by colon, like www.digital.com:80 You should not specify any method, like http:// in front of the machine name, as this command opens a TCP socket to the machine's port and the protocol has to be implemented by the BASIC program.
- #i is the file number. After the file has been opened this number has to be used in later file handling functions and commands, like CLOSE to refer to the file. The # character is optional and is allowed for compatibility with other BASIC languages. The number can be between 1 and 512.
This number is quite big for most of the applications and provides compatibility with VisualBasic.
- record_length is optional and specify the length of a record in the file. The default record length is 1 byte. File pointer setting commands usually work on records, thus SEEK, TRUNCATE and other commands and functions accept arguments or return values as number of records. The actual record length is not recorded anywhere thus the BASIC program has to remember the actual length of a record in a file. This is not a BASIC program error to open a file with a different record size than it was created, although this may certainly be a programming error.
If the file number is specified as a variable and the variable value is set to integer zero then the command will automatically find a usable file number and set the variable to hold that value. Using any other expression of value integer zero is an error.