BCX File Management Functions
FILELOCKED function
Purpose: FILELOCKED determines whether or not a file can be opened.
Syntax: RetVal% = FILELOCKED(FileName$) Parameters:
|
$FILETEST ON | OFF directive
Purpose: The $FILETEST ON or $FILETEST OFF directives specify whether or not to suppress the automatic failure checking when using OPEN and CLOSE statements.
☞ In the C translation of OPEN and CLOSE statements, BCX automatically adds code to check for the existence of the file that is being opened or closed. If the file being opened does not exist, the program will exit and a "Can't open file FileName$" statement will be sent to stdout.
Syntax: $FILETEST ON or $FILETEST OFF Parameters: |
OPEN statement
Purpose: The OPEN statement opens a file, specifies a streaming mode, and associates a file handle name with the file.
Syntax: OPEN FileName$ FOR StreamingMode AS FileHandleName Parameters:
|
Remarks:
☞ Every OPEN statement must be terminated, eventually, with a CLOSE statement. See the CLOSE section below for details.
In the C translation of OPEN and CLOSE statements, BCX automatically adds code to check for the existence of the file that is being opened or closed. If the file being opened does not exist, the program will exit and a "Can't open file FileName$" statement will be sent to stdout.
CLOSE statement
Purpose: The CLOSE statement causes the file stream associated with a file handle to be flushed and the file to be closed.
Syntax 1: CLOSE FileHandle Parameters:
|
Syntax 2: CLOSE Parameters:
|
Remarks
☞ Specifying more than one file to be closed is not allowed.
Opening Text Files
Syntax: OPEN FileName$ FOR INPUT AS hFile Streaming Mode: open text file for reading. Parameters:
|
Syntax: OPEN FileName$ FOR OUTPUT AS hFile Streaming Mode: truncate contents of existing file to zero length or create text file for writing. Parameters:
|
Syntax: OPEN FileName$ FOR APPEND AS hFile Streaming Mode: append; open or create text file for writing at end-of-file. Parameters:
|
Opening Binary Files
Syntax: OPEN FileName$ FOR BINARY AS hFile Streaming Mode: open binary file for update (reading and writing). Parameters:
|
Syntax: OPEN FileName$ FOR BINARY NEW AS hFile Streaming Mode: truncate contents of existing file to zero length or create binary file for update. Parameters:
|
Syntax: OPEN FileName$ FOR BINARY APPEND AS hFile Streaming Mode: append; open or create binary file for update, writing at end-of-file. Parameters:
|
Syntax: OPEN FileName$ FOR BINARY INPUT AS hFile Streaming Mode: open binary file for reading. Parameters:
|
Syntax: OPEN FileName$ FOR BINARY OUTPUT AS hFile Streaming Mode: open binary file for update (reading and writing). Parameters:
|
Remarks:
Expressions such as:
OPEN EXTRACT$(filename$,".") & ".bas" FOR INPUT AS FP1
are valid.
Opening Random Access Files
Syntax: OPEN fName$ FOR BINARY [NEW] AS hFile RECLEN RecordSize Parameters: |
Remarks:
If RecordSize is a TYPE structure, RECLEN will calculate automatically the size of the TYPE structure.
If RecordSize is an INTEGER variable or a literal number, that integer must specify the fixed size of the random access record.
☞ If RecordSize is an INTEGER variable, it must be appended with % integer data type signifier.
RecordSize is calculated before the value is passed to RECLEN. The programmer is responsible for determining the fixed size of the record.
For more details see the Using Random Access Files.
File Handle Names
DIM MyFileHandle@ ' BCX Reserves @ for "C" FILE* data types OPEN "new.txt" FOR OUTPUT AS FP1 MyFileHandle@ = FP1 FPRINT MyFileHandle@, "This is a test" CLOSE FP1or, for example, like this
GLOBAL DYNAMIC FHandles[12] AS FILE PTR LOCAL DYNAMIC FHandles[12] AS FILE PTR
OPEN "new.txt" FOR INPUT AS FP1 PARSE$(FP1) CLOSE FP1 FUNCTION PARSE$(ABC AS FILE) LOCAL Buffer$ LINE INPUT ABC, Buffer$ PRINT Buffer$ FUNCTION = Buffer$ END FUNCTION
In the snippet below, the file handle name MyFP1 will be LOCAL in scope to the subroutine FileTest.
SUB FileTest() DIM RAW MyFP1 AS FILE OPEN "test.txt" FOR BINARY AS MyFP1 CLOSE MyFP1 END SUB
OPEN printer
OPEN is also used to send data to a printer. OPEN will make a connection to the printer using a handle to "PRN", the standard printer communications device. The data then is output to the printer using a PRINT statement. Here is a simple example for sending data to the default printer.
DIM i OPEN "PRN" FOR OUTPUT AS FP1 FOR i = 1 TO 60 PRINT FP1,"This is line number", i NEXT PRINT FP1, CHR$(12) 'CHR$(12) is Form Feed Character CLOSE FP1
BCX Console Sample Programs using OPEN function.