long open (const char *name, long mode, long flg)


[Description]

-

Prepares for operations on the file corresponding to the filename of the first parameter. In the open routine, the file type (console, printer, disk file, etc.) must be determined in order to enable writing or reading at a later time. The file type must be referenced using the file number returned by the open routine each time reading or writing is to be performed.

-

The second parameter mode specifies processing to be performed when the file is opened. The meanings of each of the bits of this parameter are as follows.

Table 8.3

Explanation of Bits in Parameter "mode" of open Routine

mode Bit

Description

O_RDONLY (bit 0)

When this bit is 1, the file is opened in read-only mode

O_WRONLY (bit 1)

When this bit is 1, the file is opened in write-only mode

O_RDWR (bit 2)

When this bit is 1, the file is opened for both reading and writing

O_CREAT (bit 3)

When this bit is 1, if a file with the filename given does not exist, it is created

O_TRUNC (bit 4)

When this bit is 1, if a file with the filename given exists, the file contents are deleted and the file size is set to 0

O_APPEND (bit 5)

Sets the position within the file for the next read/write operation

When 0: Set to read/write from the beginning of file

When 1: Set to read/write from file end

 

-

When there is a contradiction between the file processing specified by mode and the properties of the actual file, error processing should be performed. When the file is opened normally, the file number (a positive integer) should be returned which should be used in subsequent read, write, lseek, and close routines. The correspondence between file numbers and the actual files must be managed by low-level interface routines. If the open operation fails, -1 should be returned.

[Return value]

Normal: The file number for the successfully opened file

Error: -1

[Parameters]

name Name of the file

mode Specifies the type of processing when the file is opened

flg Specifies processing when the file is opened (always 0777)