FutureBasic Logo

<<    Index    >> FutureBasic

open   statement



Syntax 1
[ result = ] open method, fileID, url [ , @err ]

Syntax 2
open method, fileID, url [ , recLen ]

Description
This statement opens a file so that you can read from it and/or write to it. If you specify a method other than "I", the open statement also creates the file if it doesn't already exist.

Special Note:Starting in FB 5.7.99, all FB I/O verbs were updated to 64-bit and the Carbon toolbox calls removed/replaced.
A replacement method for managing in use open files and notifying OPEN "N" users relies on POSIX "advisory locking".
This seems to work reliably for direct-attached local storage but it does _not_ work for server files.
Users with server files should investigate other file open methods and review the FB list thread in September/October 2017 titled "File Bug in 5.7.105"

open's parameters are described below:

method
Should be coded as either: a one character CFString or a one character pascal string.
Using the "@method" format requires the use of CFString-based I/O verbs.
Example: if your code uses line input and opens the file with "@method" ( i.e. open's syntax 1 ), it must use its CFString version ( see syntax 1 for line input ).

Using the "method" format requires the use of pascal string-based I/O verbs.
Example: if your code uses line input and opens the file with "method" ( i.e. open's syntax 2 ), it must use its CFString version ( see syntax 2 for line input ).

Same situation applies to other I/O verbs when used with open such as: read file; write file.
Look at your I/O verbs for a CFString syntax ( and a syntax 1 and syntax 2 ). Those are the ones which require a specific open method.

Coding the 'method' parameter

open @"I"     // with an '@' signifies only I/O verbs using CFStrings are permitted
open "I"      // without an '@' signifies only I/O verbs using pascal strings are permitted
IOpen for input (reading) only. The file must already exist. Other processes may read from the file (but not write to it) while it's open with this method.
OOpen for output (writing) only. If the file already exists, all of its current contents will be destroyed. You have exclusive access to the file (no other process can read from it nor write to it) while it's open with the "O" method. Does NOT support the "resource fork" open.
RSyntax 2 only.
Open for "random access." You can either read from or write to the file. The "file mark" (which indicates where the next read or write operation will occur) is placed initially at the beginning of the file. If you write to the file, you only replace those bytes which you're writing; the rest of the file's contents are unaffected. You have exclusive access to the file.
AOpen for "append." This is just like method "R", except the file mark is placed initially at the end of the file. This method is normally used when you want to add data to the end of an existing file.
NSyntax 2 only.
Open for non-exclusive random access. This is just like method "R", except that other processes may read from the file while you have it open. Your process is the only one allowed to write to the file and your code is responsible for assuring file readers don't process old or partial data. OPEN code only provides access; it does not provide any data integrity protection when there are concurrent readers and writers of the same file. If a file is already open in "N" mode, a second attempt to open in 'N' mode by the current or other process automatically provides read-only access ( essentially "I" mode ) to the file. If the process is given read-only mode, the FB runtime sends a "Permission denied" ( _EAccess/13 ) error code which can used by the caller ( must be trapped with 'on error' and "error" ). If a process is completely denied access ( such as when another app has the file open in some exclusive mode ), the error handling will report a _EAgain/35 error.

fileID
Specify a number in the range 1 through 255 which is not being used by any other currently open file. You can use this number to identify the open file in statements and functions such as read#, write, eof, lof, etc. The fileID number is associated with the file until you close the file.

url
A CFURLRef

@err
An ErrorRef (Syntax 1 only)

recLen (Syntax 2 only)
This value indicates the length of the records in the file; naturally, it's most useful when the file consists of fixed-length records. The value you specify is used when you execute statement and functions such as record, rec, loc and lof. If you omit this parameter, a default value of 256 is used. If the file doesn't consist of fixed-length records, it's often most convenient to set recLen to 1.

result
A BOOL value that indicates success or failure.

See also
close; input#; print#; read#; read file; write; write file; record; rec; loc; lof; eof