* The ''fileName$'' is a [[STRING]] variable or literal file name (path optional) in quotes.
* FOR mode can be: [[APPEND]] (write to end), [[BINARY]] (read/write), [[INPUT (file mode)|INPUT]] (read), [[OUTPUT]] (write new) or [[RANDOM]] (read/write), .
* GW Basic's ''modeletter'' is a [[STRING]] variable or the letter "A", "B", "I", "O" or "R" designating the OPEN modes above.
* ''File number'' can be any '''positive''' [[INTEGER]] or [[LONG]] whole number value or an unused value determined by the [[FREEFILE]] function.
* [[LEN]] = or ''recordLEN'' is optional to denote the RANDOM(128 default) file record byte lengths or sequential(512 default) load buffer.
{{PageDescription}}
* '''QB64''' can open as many files as your computer memory can handle. Qbasic could only open about 15 at a time.
* '''QB64 will allocate 4 bytes of memory for every possible file number up to the highest number used in a program.'''
* The '''"SCRN:"''' device is now supported in all new GL versions after April 15, 2015(see Example 3).
* '''Devices such as "KYBD:", "CONS:", "COMn" and "LPTn:" are [[Keywords currently not supported by QB64|currently NOT supported in QB64!]]'''.
: '''Note:''' OPEN "LPTn" is not supported by QB64, but may be supported directly by your operating system.
* [[OPEN COM]] can also be used for serial port access in '''QB64'''.
{{PageErrors}}
* Illegal '''QB64''' Windows filename characters are ''' " * / \ | ? : < > '''. Multiple dots(periods) are allowed, but only the first one will be set.
* Illegal Qbasic Windows filename characters are ''' " * / \ + | ? [ ] , ; : < > ''' and more than one dot(period).
* Possible OPEN [[ERROR Codes|errors]] include "Bad file name or number", "Bad File Mode", "File Not Found" or "Path Not Found".
::: An OPEN file not found error may occur if [[CHR$]](0) to (31) are used in a Windows file name!
* Qbasic ''filenames'' must not exceed 12 characters(including dot) with a maximum of 3 file type extension characters using the DOS 8.3 naming convention limits. '''QB64''' does not have those file name limitations.
* [[OUTPUT]]: Sequential mode creates a new file or erases an existing file for new program output. Use [[WRITE (file statement)|WRITE #]] to write numerical or text data or the [[PRINT (file statement)|PRINT #]] for text. '''OUTPUT clears files of all data''' and clears the receive buffer on other devices such as [[ON COM(n)|COM]].
* [[APPEND]]: Sequential mode creates a new file if it doesn't exist or appends program output to the end of an existing file. Use [[WRITE (file statement)|WRITE #]] for numerical or text data or the [[PRINT (file statement)|PRINT #]] for text as in the OUTPUT mode. '''APPEND does not remove previous data.'''
* [[INPUT (file mode)|INPUT]] : Sequential mode '''only reads input''' from an existing file. '''[[ERROR Codes|File error]] if file does not exist!''' Use [[INPUT (file statement)|INPUT #]] for comma separated numerical or text data and [[LINE INPUT (file statement)|LINE INPUT #]] or [[INPUT$]] to only read text data. '''Use [[_FILEEXISTS]] or [[_DIREXISTS]] to avoid errors.'''
* [[BINARY]]: Creates a new file when it doesn't exist or reads and writes to an existing binary file. Use [[GET|GET #]] to read or [[PUT|PUT #]] to write byte positions simultaneously. [[LEN]] = statements are ignored in this mode only.
* [[RANDOM]]: Creates a new file when it doesn't exist or reads or writes to an existing random file record. Use [[GET|GET #]] or [[PUT|PUT #]] to read or write to file records. A [[LEN]] = statement can define the byte size of a record (no LEN statement defaults to 128 bytes)
::* "A" [[APPEND]] sequential mode allows data to be appended to an existing file using [[PRINT (file statement)|PRINT]] or [[WRITE (file statement)|WRITE]].
::* "B" [[BINARY]] byte mode allows data to be read or written using [[GET]] or [[PUT]]
::* "I" [[INPUT (file mode)|INPUT]] sequential mode allows data to be read using [[INPUT (file statement)|INPUT]] or [[LINE INPUT (file statement)|LINE INPUT]] in '''existing''' files only.
::* "O" [[OUTPUT]] sequential mode creates or clears a file to write new data using [[PRINT (file statement)|PRINT]] or [[WRITE (file statement)|WRITE]].
::* "R" [[RANDOM]] record mode allows [[TYPE]] or [[FIELD]] records to be read or written using [[GET]] or [[PUT]].
:* ''File name'' can be a variable or literal [[STRING]] file name value or port or device.
:* The '''"SCRN:"''' device is now supported in all new GL versions after April 15, 2015: {{text|'''OPEN "O", #1, "SCRN:"'''|green}}
:* '''Devices such as "KYBD:", "CONS:", "COMn" and "LPTn:" as file names are [[Keywords currently not supported by QB64|currently NOT supported in QB64!]]'''.
:: '''Note:''' OPEN "O", #1, "LPTn" is not supported by QB64, but may be supported directly by your operating system.
:* ''RecordLEN'' can be a variable or literal [[INTEGER]] value used in "R" mode only.
:* This type of OPEN allows the statement to be made using program variables only. A holdover for compatibility with GW Basic.
:* '''Note:''' Does not support any file [[ACCESS]] or [[LOCK]] restrictions.
<center> '''Comparing the GWBasic OPEN to a Qbasic OPEN statement:'''</center>
::::::::::GWBasic: OPEN "A", #1, Filename$
::::::::::Qbasic: OPEN Filename$ FOR APPEND AS #1
:Where Filename$ is the filename variable or a literal string name such as "Data1.DAT" is used. The Qbasic syntax cannot use a variable to change the OPEN mode so the programmer must determine it ahead of time.
''Example 1:'' Function that displays errors and the number of errors in Qbasic filenames. Returns 0 when filename is OK.