%open() Function

The %open() function opens the host OS file specified by str and sets the file status flags according to the value of oflag.

Syntax

file.descriptor = %open(str, oflag , pmode)

Parameter(s)

str

Specifies the file name to open or create.

oflag

Specifies the status of the file. Valid values are defined in the include file: fcntl.h. Multiple flags can be specified in the syntax.

O$APPEND

Moves the file pointer to the end of the file.

O$BINARY

Opens in binary mode.

O$CREAT

Creates a new file with the name specified in str. Owner-ID and group-ID are set and the mode of the file is set according to the value of mode modified as follows: all bits in the file mode creation mask of the process are cleared and the sticky bit is cleared. If mode is not specified, the file is opened with read and write permissions.

O$EXCL

If O$EXCL and O$CREAT are set, open fails if the file exists.

O$NDELAY

Nonblocking I/O. The effect of this flag varies depending on the type of the file. See host OS system documentation.

O$RDONLY

Opens for reading only.

O$RDWR

Opens for reading and writing.

O$SYNC

Synchronized writes.

O$TEXT

Opens in text mode and translates text (default). For example, ^Z is assumed to be an end of line.

O$TRUNC

If the file exists, its length is set to 0.

O$WRONLY

Opens for writing only.

pmode

Specifies the read-write permissions for newly created files opened with the O$CREAT flag. Valid values are defined in the include file: mode.h. If no value is specified, the file is opened with read and write permissions.

S$IRUSR

Opens a new file with read only permissions. (UNIX)

S$IWUSR

Opens a new file with write only permissions. (UNIX)

S$IREAD

Opens a new file with read only permissions. (Windows)

S$IWRITE

Opens a new file with write only permissions. (Windows)

Description

Files opened by %open() are closed automatically when the FlashBASIC program terminates.

The file descriptor is returned as a number or -1 if an error occurred. system(0) contains the error number.

NOTE

  • There is a limit on the number of host OS files a process can have opened simultaneously. See your platform documentation.

  • Files opened by a FlashBASIC program are normally closed automatically when the program terminates. However, it is a good practice to close them.

Example(s)

include dm,bp,unix.h fcntl.h

fd=%open(’fname’, o$wronly+o$append)

if fd<0 then

crt ’Cannot open. errno=’:system(0)

end

See Also

%close() Function

%fsize() Function

%lseek() Function

%rdhex() Function

%read() Function

%ttyname() Function

%unlink ()Function

%whex() Function

%write() Function

FlashBASIC C Functions Overview

cfunction Statement