From QB64 Wiki
The OPEN statement is used to open a file or COM serial communications port for program input or output.
GW Basic Syntax:
- OPEN modeletter$, [#]filenumber, filename$[, recordLEN]
- 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 (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.
- 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 "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 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.
- 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 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.
- ACCESS clause limits file access to READ, WRITE or READ WRITE on a network ONLY with DOS 3.1 or greater.
- 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.
- OUTPUT: Sequential mode creates a new file or erases an existing file for new program output. Use WRITE # to write numerical or text data or the PRINT # for text. OUTPUT clears files of all data and clears the receive buffer on other devices such as 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 # for numerical or text data or the PRINT # for text as in the OUTPUT mode. APPEND does not remove previous data.
- INPUT : Sequential mode only reads input from an existing file. File error if file does not exist! Use INPUT # for comma separated numerical or text data and 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 # to read or 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 # or 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, BINARY and RANDOM file modes allow a file to be concurrently opened in a different mode and number.
- Mode letter is a variable or literal STRING letter value as one of the following:
- "A" APPEND sequential mode allows data to be appended to an existing file using PRINT or WRITE.
- "B" BINARY byte mode allows data to be read or written using GET or PUT
- "I" INPUT sequential mode allows data to be read using INPUT or LINE INPUT in existing files only.
- "O" OUTPUT sequential mode creates or clears a file to write new data using PRINT or WRITE.
- "R" 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 "SCRN:" device is now supported in all new GL versions after April 15, 2015: OPEN "O", #1, "SCRN:"
- Devices such as "KYBD:", "CONS:", "COMn" and "LPTn:" as file names are currently NOT supported in QB64!.
- Note: OPEN "O", #1, "LPTn" is not supported by QB64, but may be supported directly by your operating system.
- 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.
file$ = "Hello,~1.mp3" 'example call below LOCATE 20, 30: errors% = CheckName%(file$): COLOR 14: PRINT " Total Errors ="; errors% FUNCTION CheckName% (Filename$) 'NOTE: Function also displays filename errors so LOCATE on screen before call! DIM L AS INTEGER, DP AS INTEGER, XL AS INTEGER L = LEN(Filename$): DP = INSTR(Filename$, "."): IF DP THEN XL = L - DP 'extension IF L = 0 OR L > 12 OR DP > 9 OR XL > 3 THEN CheckName% = -1: COLOR 12: PRINT "Illegal format!"; : EXIT FUNCTION END IF FOR i% = 1 TO L 'check each filename character" code% = ASC(MID$(Filename$, i%, 1)): COLOR 10 ' see ASCII codes SELECT CASE code% 'check for errors and highlight in red CASE 34, 42 TO 44, 47, 58 TO 63, 91 TO 93, 124: E% = E% + 1: COLOR 12 ' Qbasic errors 'CASE 34, 42, 47, 58, 60, 62, 92, 124: E% = E% + 1: COLOR 12 ' QB64 errors CASE 46: dot% = dot% + 1: IF dot% > 1 THEN E% = E% + 1: COLOR 12 END SELECT PRINT CHR$(code%); 'use LOCATE before FUNCTION call to place print NEXT CheckName% = E% END FUNCTION
Note: The QB64 character error list is commented out. Comment out the Qbasic one when using the QB64 list.
Hello,~1.mp3 Total Errors = 1
- 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.
INPUT "Enter a file name: ", file$ IF Exist%(file$) THEN OPEN file$ FOR INPUT AS #1: found% = -1 'function call demo CLOSE #1 IF found% THEN PRINT "File exists!" ELSE PRINT "File not found!" END FUNCTION Exist% (filename$) f% = FREEFILE OPEN filename$ FOR APPEND AS #f% IF LOF(f%) THEN Exist% = -1 ELSE Exist% = 0: CLOSE #f%: KILL filename$ CLOSE #f% END FUNCTION
Example 3: When OPEN "SCRN:" FOR OUTPUT AS #f is used, PRINT #f will print the text to the screen instead of to a file:
f% = FREEFILE 'should always be 1 at program start OPEN "SCRN:" FOR OUTPUT AS #f% g% = FREEFILE 'should always be 2 after 1 OPEN "temp.txt" FOR OUTPUT AS #g% FOR i = 1 TO 2 PRINT #i, "Hello World, Screen and File version" NEXT
- Note: Linux or Mac file names can use a path destination such as ".\SCRN:" to use SCRN: as an actual file name.
- QB64 can use the _OPENCLIENT, _OPENHOST or _OPENCONNECTION functions for TCP/IP internet connections.
- 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 (TCP/IP)
- _SNDOPEN, _LOADIMAGE
- Port Access Libraries (COM or LPT registers)