open

Opens a file or stream

Local Servers:blocking
Remote Servers:blocking
Classification:POSIX
Platform:NLM
Service:Operating System I/O

Syntax

  #include <fcntl.h>  
   
  int open  (  
     const char   *path,   
     int           oflag,   
     ...);
  

Parameters

path
(IN) Points to the name of the file to open (considered within the context of the currently set namespace).
oflag
(IN) Specifies the access mode.

Return Values

When an error occurs while opening the file, a value of -1 is returned. Otherwise, an integer (not equal to -1), known as the file handle, is returned to be used with the other functions that operate on the file.

If an error occurs, errno can be set to:

Decimal

Constant

Description

1

ENONENT

No such file.

6

EACCES

Permission denied.

9

EINVAL

Invalid argument.

When an error occurs, NetWareErrno is set to:

Decimal

Hex

Constant

108

(0x6C)

ERR_BAD_ACCESS

152

(0x98)

ERR_INVALID_VOLUME

156

(0x9C)

ERR_INVALID_PATH

Remarks

This function also works on the DOS partition.

This function allows for as many open files as there is available memory. The path parameter supplies the name of the file to be opened. The file is accessed according to the access mode specified by the oflag parameter.

The access mode is established as a combination of the bits defined in the FCNTL.H header file. The following bits can be set:

O_RDONLY

Permits the file to be only read.

O_WRONLY

Permits the file to be only written.

O_RDWR

Permits the file to be both read and written.

O_APPEND

Causes each record that is written to be written at the end of the file.

O_CREAT

Has no effect when the file indicated by the file name parameter already exists; otherwise, the file is created.

O_TRUNC

Causes the file to be truncated to contain no data when the file exists; has no effect when the file does not exist. O_TRUNC must be ORed with write access to truncate a file:

O_TRUNC | O_RDWR

O_TRUNC | O_WRONLY

O_BINARY

Causes the data to be transmitted unchanged. (Text mode for first-level handles is not supported. In text mode, carriage- return/line-feed pairs are translated to line feeds on input, and line feeds are translated to carriage-return/line-feed pairs on output.)

O_CREAT must be specified when the file does not exist and it is to be written.

An optional third parameter, int permission, is used when the file is to be created (O_CREAT is specified) to set file permissions. File permissions are set according to the value contained in the permission parameter. The access permissions for the file is specified as a combination of bits (defined in the SYS\STAT.H header file).

S_IWRITE

The file is writable.

S_IREAD

The file is readable.

The permission parameter can be specified as S_IWRITE, S_IREAD or S_IWRITE|S_IREAD. Specifying 0 also makes a file both writable and readable.

When opening a UNIX Stream file, access must be constructed from O_NDELAY and either O_RDONLY, O_WRONLY, or O_RDWR. Other flag values are not applicable to Stream devices and have no effect on them. The value of O_NDELAY affects the operation of Stream drivers and certain function calls ( read, getmsg, putmsg, and write). For drivers, the implementation of O_NDELAY is device-specific. Each Stream device driver can treat this option differently.

SetCurrentNameSpace sets the name space which is used for parsing the path input to this function.

See Also

close, creat, dup, dup2, eof, filelength, fileno, fstat, isatty, lseek, read, sopen, tell, write

Example

  #include <stddef.h>  
  #include <fcntl.h>  
  #include <errno.h>  
   
  main()  
  {  
     int fh,size;  
     char buffer[] = {"a text record to be written\n" };  
     fh = open ("test.dat",O_WRONLY | O_CREAT | O_TRUNC,0);  
     printf ("handle: %d\n\r",fh);  
     if(fh == EFAILURE)  
     {  
        printf ("could not open file\r\n");  
        goto end;  
     }  
     printf ("%d\n\r",tell(fh));  
     size = write (fh,buffer,sizeof(buffer));  
     if(size < 29)  
     {  
        printf ("could not write to file\r\n");  
        goto end;  
     }  
     printf ("%d\r\n",tell(fh));  
     close (fh);  
     end:  
     printf ("%d\r\n",errno);  
     getch ();  
  }