The '''OPEN''' statement is used to open a file or [[OPEN_COM|COM]] serial communications port for program input or output.


''Qbasic:'' {{PageSyntax}}

::'''OPEN''' ''FileName$'' ['''FOR''' mode] [{{{KW|ACCESS}}|{{{KW|LOCK}}|SHARED}} [{READ|WRITE}] '''AS''' [#]''FileNumber&amp;'' [LEN = ''recordLEN'']


''GW Basic'' {{PageSyntax}} 
::'''OPEN''' ''modeletter$'', [#]''filenumber'', ''filename$''[, ''recordLEN'']


''Parameters:''
* 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 &quot;A&quot;, &quot;B&quot;, &quot;I&quot;, &quot;O&quot; or &quot;R&quot; 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 ''mode'' defaults to RANDOM if the ''mode'' or FOR access statement is omitted. (see open modes described below)
* '''Only the ''filename'', ''file number'' and LEN = ''record length'' values can use variable values in the Qbasic OPEN syntax.'''
* If [[LEN]] = is ommitted, sequential file record sizes default to 512 and [[RANDOM]] to 128 bytes in Qbasic.
* '''QB64''' ''filenames'' can be up to 255(Windows) characters with no limit on file name extension length. 
* Once a file or port is opened, it can be used in any program procedure using the assigned file number. 
* The '''&quot;SCRN:&quot;''' device is now supported in all new GL versions after April 15, 2015(see Example 3).
* '''Devices such as &quot;KYBD:&quot;, &quot;CONS:&quot;, &quot;COMn&quot; and &quot;LPTn:&quot; are [[Keywords currently not supported by QB64|currently NOT supported in QB64!]]'''. 
: '''Note:''' OPEN &quot;LPTn&quot; 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 ''' &quot; * / \ | ? : &lt; &gt; '''. Multiple dots(periods) are allowed, but only the first one will be set. 
* Illegal Qbasic Windows filename characters are ''' &quot; * / \ + | ? [ ] , ; : &lt; &gt; ''' and more than one dot(period). 
* Possible OPEN [[ERROR Codes|errors]] include &quot;Bad file name or number&quot;, &quot;Bad File Mode&quot;, &quot;File Not Found&quot; or &quot;Path Not Found&quot;.
::: 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.


&lt;center&gt; ''' File ACCESS and LOCK Permissions'''&lt;/center&gt;

* [[ACCESS]] clause limits file access to READ, WRITE or READ WRITE on a network ONLY with DOS 3.1 or greater.
* [[LOCK (access)|LOCK]] clause can specify SHARED or a LOCK READ or LOCK WRITE file lock in an OPEN statement working on a network.
* A separate [[LOCK]] statement can lock or [[UNLOCK]] file access on a network ONLY using a format that can lock specific records.
* ''Note:'' '''Qbasic''' ACCESS and LOCK clauses required that the DOS '''SHARED.EXE''' program be run for networking access.


&lt;center&gt;'''The 5 Qbasic File Access Modes:'''&lt;/center&gt;

* [[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)

* The [[INPUT (file mode)|INPUT]], [[BINARY]] and [[RANDOM]] file modes allow a file to be concurrently opened in a different mode and number.


&lt;center&gt;'''GW Basic OPEN statements'''&lt;/center&gt;
:* ''Mode letter'' is a variable or literal [[STRING]] letter value as one of the following:

::* &quot;A&quot; [[APPEND]] sequential mode allows data to be appended to an existing file using [[PRINT (file statement)|PRINT]] or [[WRITE (file statement)|WRITE]].
::* &quot;B&quot; [[BINARY]] byte mode allows data to be read or written using [[GET]] or [[PUT]]
::* &quot;I&quot; [[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.
::* &quot;O&quot; [[OUTPUT]] sequential mode creates or clears a file to write new data using [[PRINT (file statement)|PRINT]] or [[WRITE (file statement)|WRITE]].
::* &quot;R&quot; [[RANDOM]] record mode allows [[TYPE]] or [[FIELD]] records to be read or written using [[GET]] or [[PUT]].


:* ''File number'' can be any variable or literal [[INTEGER]] value between 1 and 255 or a  [[FREEFILE]] value.
:* ''File name'' can be a variable or literal [[STRING]] file name value or port or device.
:* The '''&quot;SCRN:&quot;''' device is now supported in all new GL versions after April 15, 2015: {{text|'''OPEN &quot;O&quot;, #1, &quot;SCRN:&quot;'''|green}}
:* '''Devices such as &quot;KYBD:&quot;, &quot;CONS:&quot;, &quot;COMn&quot; and &quot;LPTn:&quot; as file names are [[Keywords currently not supported by QB64|currently NOT supported in QB64!]]'''. 
:: '''Note:''' OPEN &quot;O&quot;, #1, &quot;LPTn&quot; 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 &quot;R&quot; 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.


&lt;center&gt; '''Comparing the GWBasic OPEN to a Qbasic OPEN statement:'''&lt;/center&gt;


::::::::::GWBasic: OPEN &quot;A&quot;, #1, Filename$

::::::::::Qbasic: OPEN Filename$ FOR APPEND AS #1

:Where Filename$ is the filename variable or a literal string name such as &quot;Data1.DAT&quot; 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.

{{CodeStart}}
 file$ = &quot;Hello,~1.mp3&quot;      'example call below
 {{Cl|LOCATE}} 20, 30: errors% = CheckName%(file$): {{Cl|COLOR}} 14: {{Cl|PRINT}} &quot;  Total Errors =&quot;; errors% 

{{Cl|FUNCTION}} CheckName% (Filename$)
  '{{Cl|NOT}}E: Function also displays filename errors so {{Cl|LOCATE}} on screen before call!
  {{Cl|DIM}} L {{Cl|AS}} {{Cl|INTEGER}}, DP {{Cl|AS}} {{Cl|INTEGER}}, XL {{Cl|AS}} {{Cl|INTEGER}}
  L = {{Cl|LEN}}(Filename$): DP = {{Cl|INSTR}}(Filename$, &quot;.&quot;): {{Cl|IF...THEN|IF}} DP {{Cl|THEN}} XL = L - DP 'extension
  {{Cl|IF...THEN|IF}} L = 0 {{Cl|OR (boolean)|OR}} L &gt; 12 {{Cl|OR (boolean)|OR}} DP &gt; 9 {{Cl|OR (boolean)|OR}} XL &gt; 3 {{Cl|THEN}} 
    CheckName% = -1: {{Cl|COLOR}} 12: {{Cl|PRINT}} &quot;Illegal format!&quot;; : {{Cl|EXIT FUNCTION}}
  {{Cl|END IF}}
  {{Cl|FOR...NEXT|FOR}} i% = 1 {{Cl|TO}} L      'check each filename character&quot;
     code% = {{Cl|ASC}}({{Cl|MID$}}(Filename$, i%, 1)): {{Cl|COLOR}} 10      ' see ASCII codes
     {{Cl|SELECT CASE}} code%       'check for errors and highlight in red
	{{Cl|CASE}} 34, 42 {{Cl|TO}} 44, 47, 58 {{Cl|TO}} 63, 91 {{Cl|TO}} 93, 124: E% = E% + 1: {{Cl|COLOR}} 12 ' '''Qbasic errors'''
       '{{Cl|CASE}} 34, 42, 47, 58, 60, 62, 92, 124: E% = E% + 1: {{Cl|COLOR}} 12 ' '''QB64 errors'''
        {{Cl|CASE}} 46: dot% = dot% + 1: {{Cl|IF...THEN|IF}} dot% &gt; 1 {{Cl|THEN}} E% = E% + 1: {{Cl|COLOR}} 12
     {{Cl|END SELECT}}     
     {{Cl|PRINT}} {{Cl|CHR$}}(code%);  'use {{Cl|LOCATE}} before {{Cl|FUNCTION}} call to place print
  {{Cl|NEXT}}  
  CheckName% = E%
{{Cl|END FUNCTION}} '' ''
{{CodeEnd}}
''Note: The QB64 character error list is commented out. Comment out the Qbasic one when using the QB64 list.

{{OutputStart}}
                         {{text|Hello|#54FC54}}{{text|,|red}}{{text|~1.mp3|#54FC54}}  {{text|Total Errors|yellow}}&lt;nowiki&gt; = &lt;/nowiki&gt;{{text|1|yellow}}
{{OutputEnd}}
:''Note:'' The screen output displays filename characters in green except for red comma Qbasic error.


''Example 2:'' A function that verifies that a file exists if it is NOT empty. Note: May create a file that is deleted if empty.
{{CodeStart}} '' ''
{{Cl|INPUT}} &quot;Enter a file name: &quot;, file$
{{Cl|IF}} Exist%(file$) {{Cl|THEN}} {{Cl|OPEN}} file$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1: found% = -1   'function call demo
{{Cl|CLOSE}} #1
{{Cl|IF}} found% THEN {{Cl|PRINT}} &quot;File exists!&quot; {{Cl|ELSE}} {{Cl|PRINT}} &quot;File not found!&quot;
{{Cl|END}}

{{Cl|FUNCTION}} Exist% (filename$)
f% = {{Cl|FREEFILE}}
{{Cl|OPEN}} filename$ {{Cl|FOR (file statement)|FOR}} {{Cl|APPEND}} {{Cl|AS}} #f%
{{Cl|IF}} {{Cl|LOF}}(f%) {{Cl|THEN}} Exist% = -1 {{Cl|ELSE}} Exist% = 0: {{Cl|CLOSE}} #f%: {{Cl|KILL}} filename$
{{Cl|CLOSE}} #f% 
{{Cl|END FUNCTION}} '' ''
{{CodeEnd}}
{{small|Code by Ted Weissgerber}}


''Example 3:'' When '''OPEN &quot;SCRN:&quot; FOR OUTPUT AS #f''' is used, '''PRINT #f''' will print the text to the screen instead of to a file:
{{CodeStart}} '' ''
f% = {{Cl|FREEFILE}} 'should always be 1 at program start
{{Cl|OPEN}} &quot;SCRN:&quot; {{Cl|FOR...NEXT|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #f%
g% = {{Cl|FREEFILE}} 'should always be 2 after 1
{{Cl|OPEN}} &quot;temp.txt&quot; {{Cl|FOR...NEXT|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #g%

{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} 2
    {{Cl|PRINT (file statement)|PRINT}} #i, &quot;Hello World, Screen and File version&quot;
NEXT '' ''
{{CodeEnd}}{{small|GL implementation by Steve McNeill April 15, 2015}}
: ''Note:'' Linux or Mac file names can use a path destination such as &quot;.\SCRN:&quot; to use SCRN: as an actual file name. 


* '''QB64''' can use the [[_OPENCLIENT]], [[_OPENHOST]] or [[_OPENCONNECTION]] functions for TCP/IP internet connections. 



''See also:'' 
* [[PRINT (file statement)]], [[INPUT (file statement)]]
* [[GET]], [[PUT]], [[WRITE (file statement)]] 
* [[INPUT$]], [[LINE INPUT (file statement)]]
* [[CLOSE]], [[LOF]], [[EOF]], [[LOC]]
* [[SEEK (statement)]], [[SEEK]]
* [[OPEN COM]], [[LEN]], [[RESET]] 
* [[FILEATTR]], [[FIELD]], [[TYPE]]
* [[_FILEEXISTS]], [[_DIREXISTS]]
* [[_OPENCLIENT]], [[_OPENHOST]], [[_OPENCONNECTION]] {{text|(TCP/IP)}}
* [[_SNDOPEN]], [[_LOADIMAGE]]
* [[Port Access Libraries]] {{text|(COM or LPT registers)}}


{{PageNavigation}}