QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

The OPEN statement is used to open a file or OPEN COM serial communications port for program input or output.

Syntax

OPEN fileName$ [FOR mode] [{ACCESS {LOCK SHARED}} [{READ WRITE}] AS [#]fileNumber& [LEN = recordLength]

Legacy GW-BASIC syntax

OPEN modeLetter$, [#]fileNumber&, fileName$[, recordLength]

Parameter(s)

Description

Error(s)

Details

File ACCESS and LOCK Permissions

File Access Modes

GW-BASIC modes

Example(s)

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 QBasic character error list is commented out and the function will return invalid filenames under QB64.


                         Hello,~1.mp3  Total Errors<nowiki> = </nowiki>1

Note: The screen output displays filename characters in green except for red comma QBasic error.

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.

Showcasing different file modes.


CLS

OPEN "test.tst" FOR OUTPUT AS #1
PRINT #1, "If test.tst didn't exist:"
PRINT #1, "A new file was created named test.tst and then deleted."
PRINT #1, "If test.tst did exist:"
PRINT #1, "It was overwritten with this and deleted."
CLOSE #1

OPEN "test.tst" FOR INPUT AS #1
DO UNTIL EOF(1)
INPUT #1, a$
PRINT a$
LOOP
CLOSE #1

KILL "test.tst"

END


If test.tst didn't exist:
A new file was created named test.tst and then deleted.
If test.tst did exist:
It was overwritten with this and deleted.

Warning: Make sure you don’t have a file named test.tst before you run this or it will be overwritten.

See Also