open
Opens a file or stream
#include <fcntl.h>
int open (
const char *path,
int oflag,
...);
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:
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_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).
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.
close, creat, dup, dup2, eof, filelength, fileno, fstat, isatty, lseek, read, sopen, tell, write
#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 ();
}