INPUT (file mode) - mkilgore/QB64pe GitHub Wiki

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

Syntax

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

Legacy GW-BASIC syntax

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

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 (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.
  • fileNumber& can be any positive INTEGER or LONG whole number value or an unused value determined by the FREEFILE function.
  • LEN = or recordLength is optional to denote the RANDOM file record byte length (default = 128) or sequential (default = 512) load buffer.

Description

  • 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.
  • mode defaults to RANDOM if the mode or FOR access statement is omitted. (see open modes described below)
  • Only the fileName$, fileNumber& and LEN = recordLength values can use variable values in the QBasic syntax.
  • If LEN = is ommitted, sequential file record sizes default to 512 and RANDOM to 128 bytes in Qbasic.
  • fileName$ can be up to 255 characters with no limit on file name extension length in QB64.
  • Once a file or port is opened, it can be used in any program procedure using the assigned file number.
  • The "SCRN:" device is supported in version 1.000 and up (see Example 3).
  • Devices such as "KYBD:", "CONS:", "COMn" and "LPTn:" are 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.

Errors

  • Illegal QB64 Windows filename characters are " * / \ | ? : < > . Multiple dots (periods) are allowed.
  • 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.
  • QB64 does not have DOS file name limitations.

Details

File ACCESS and LOCK Permissions

  • ACCESS clause limits file access to READ, WRITE or READ WRITE on a network.
  • 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 using a format that can lock specific records.
  • If another process already has access to a specified file, program access is denied for that file OPEN access. A "Permission Denied" error 70 will be returned. A network program must be able to handle a denial of access error.

File Access Modes

  • FOR mode can be:
    • 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 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 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.
    • 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)
    • Modes INPUT, BINARY and RANDOM allow a file to be concurrently opened in a different mode and number.

GW-BASIC modes

  • Mode letter is a variable or literal STRING letter value as one of the following:
    • "A" = APPEND.
    • "B" = BINARY.
    • "I" = INPUT.
    • "O" = OUTPUT.
    • "R" = RANDOM.

Examples

Example 1: Function that displays errors and the number of errors in QBasic filenames. Returns 0 when filename is OK.

FUNCTION CheckName% (Filename$)
  &#39;NOTE&#58; Function also displays filename errors so LOCATE on screen before call&#33;
  DIM L AS INTEGER, DP AS INTEGER, XL AS INTEGER
  L &#61; LEN(Filename$)&#58; DP &#61; INSTR(Filename$, &quot;.&quot;)&#58; IF...THEN DP THEN XL &#61; L &#45; DP &#39;extension
  IF...THEN L &#61; 0 OR (boolean) L &gt; 12 OR (boolean) DP &gt; 9 OR (boolean) XL &gt; 3 THEN 
    CheckName% &#61; &#45;1&#58; COLOR 12&#58; PRINT &quot;Illegal format&#33;&quot;&#59; &#58; EXIT FUNCTION
  END IF
  FOR...NEXT i% &#61; 1 TO L      &#39;check each filename character&quot;
     code% &#61; ASC(MID$(Filename$, i%, 1))&#58; COLOR 10      &#39; see ASCII codes
     SELECT CASE code%       &#39;check for errors and highlight in red
	&#39;CASE 34, 42 TO 44, 47, 58 TO 63, 91 TO 93, 124&#58; E% &#61; E% + 1&#58; COLOR 12 &#39; &#39;&#39;&#39;QBasic errors&#39;&#39;&#39;
        CASE 34, 42, 47, 58, 60, 62, 92, 124&#58; E% &#61; E% + 1&#58; COLOR 12 &#39; &#39;&#39;&#39;QB64 errors&#39;&#39;&#39;
        CASE 46&#58; dot% &#61; dot% + 1&#58; IF...THEN dot% &gt; 1 THEN E% &#61; E% + 1&#58; COLOR 12
     END SELECT     
     PRINT CHR$(code%)&#59;  &#39;use LOCATE before FUNCTION call to place print
  NEXT  
  CheckName% &#61; E%
END FUNCTION &#39;&#39; &#39;&#39;
Note: The QBasic character error list is commented out and the function will return invalid filenames under QB64.
                         &lt;span style&#61;&quot;color&#58;&#35;54FC54&#59;&quot;&gt;Hello&lt;/span&gt;&lt;span style&#61;&quot;color&#58;red&#59;&quot;&gt;,&lt;/span&gt;&lt;span style&#61;&quot;color&#58;&#35;54FC54&#59;&quot;&gt;~1.mp3&lt;/span&gt;  &lt;span style&#61;&quot;color&#58;yellow&#59;&quot;&gt;Total Errors&lt;/span&gt; &#61; &lt;span style&#61;&quot;color&#58;yellow&#59;&quot;&gt;1&lt;/span&gt;
Note: The screen output displays filename characters in green except for red comma QBasic error.
Example 2: When OPEN "SCRN:" FOR OUTPUT AS #f is used, PRINT #f will print the text to the screen instead of to a file:
FOR...NEXT i &#61; 1 TO 2
    PRINT (file statement) &#35;i, &quot;Hello World, Screen and File version&quot;
NEXT &#39;&#39; &#39;&#39;
code by Steve McNeill
Note: Linux or Mac file names can use a path destination such as ".\SCRN:" to use SCRN: as an actual file name.
Example 3: Showcasing different file modes.
OPEN &quot;test.tst&quot; FOR (file statement) OUTPUT AS &#35;1
PRINT (file statement) &#35;1, &quot;If test.tst didn&#39;t exist&#58;&quot;
PRINT (file statement) &#35;1, &quot;A new file was created named test.tst and then deleted.&quot;
PRINT (file statement) &#35;1, &quot;If test.tst did exist&#58;&quot;
PRINT (file statement) &#35;1, &quot;It was overwritten with this and deleted.&quot;
CLOSE &#35;1

OPEN &quot;test.tst&quot; FOR (file statement) INPUT (file mode) AS &#35;1
DO UNTIL EOF(1)
INPUT (file statement) &#35;1, a$
PRINT a$
LOOP
CLOSE &#35;1

KILL &quot;test.tst&quot;

END

If test.tst didn&#39;t exist&#58;
A new file was created named test.tst and then deleted.
If test.tst did exist&#58;
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


Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page
⚠️ **GitHub.com Fallback** ⚠️