Opens a file.
#include <fcntl.h> | ||
#include <sys\types.h> | ||
#include <sys\stat.h> | ||
#include <io.h> |
int _open( char *filename, int oflag [[, int pmode]] );
filename | Filename | |
oflag | Type of operations allowed | |
pmode | Permission mode |
The _open function opens the file specified by filename and prepares the file for subsequent reading or writing, as defined by oflag. The oflag argument is an integer expression formed from one or more of the manifest constants defined in FCNTL.H (listed below). When two or more manifest constants are used to form the oflag argument, the constants are combined with the bitwise-OR operator (|). See “File Handling” for a discussion of binary and text modes.
The FCNTL.H file defines the following manifest constants:
Constant | Meaning |
_O_APPEND | Repositions the file pointer to the end of the file before every write operation. |
_O_BINARY | Opens file in binary (untranslated) mode. |
_O_CREAT | Creates and opens a new file for writing; this has no effect if the file specified by filename exists. |
_O_EXCL | Returns an error value if the file specified by filename exists. Only applies when used with _O_CREAT. |
_O_RDONLY | Opens file for reading only; if this flag is given, neither _O_RDWR nor _O_WRONLY can be given. |
_O_RDWR | Opens file for both reading and writing; if this flag is given, neither _O_RDONLY nor _O_WRONLY can be given. |
_O_TEXT | Opens file in text (translated) mode. |
_O_TRUNC | Opens and truncates an existing file to zero length; the file must have write permission. The contents of the file are destroyed. If this flag is given, you cannot specify _O_RDONLY. |
_O_WRONLY | Opens file for writing only; if this flag is given, neither _O_RDONLY nor _O_RDWR can be given. |
Warning!:
Use the _O_TRUNC flag with care, as it destroys the complete contents of an existing file.
Either _O_RDONLY, _O_RDWR, or _O_WRONLY must be given to specify the access mode. There is no default value for the access mode.
The pmode argument is required only when _O_CREAT is specified. If the file exists, pmode is ignored. Otherwise, pmode specifies the file's permission settings, which are set when the new file is closed for the first time. The pmode is an integer expression containing one or both of the manifest constants _S_IWRITE and _S_IREAD, defined in SYS\STAT.H. When both constants are given, they are joined with the bitwise-OR operator (|). The meaning of the pmode argument is as follows:
Value | Meaning |
_S_IWRITE | Writing permitted |
_S_IREAD | Reading permitted |
_S_IREAD | _S_IWRITE | Reading and writing permitted |
If write permission is not given, the file is read-only. With DOS, all files are readable; it is not possible to give write-only permission. Thus the modes _S_IWRITE and _S_IREAD | _S_IWRITE are equivalent.
The _open function applies the current file-permission mask to pmode before setting the permissions (see _umask).
The filename argument used in the _open function is affected by the DOS APPEND command.
Note that with DOS versions 3.0 and later, a problem occurs when SHARE is installed and a new file is opened with oflag set to _O_CREAT | _O_RDONLY or _O_CREAT | _O _WRONLY and pmode set to _S_IREAD. Under these conditions, the operating system prematurely closes the file during system calls made within _open.
To work around the problem, open the file with the pmode argument set to _S_IWRITE. Then close the file and use _chmod to change the access mode back to _S_IREAD. Another workaround is to open the file with pmode set to _S_IREAD and oflag set to _O_CREAT | _O_RDWR.
The _open function returns a file handle for the opened file. A return value of –1 indicates an error, and errno is set to one of the following values:
Value | Meaning |
EACCES | Given path name is a directory; or an attempt was made to open a read-only file for writing; or a sharing violation occurred (the file's sharing mode does not allow the specified operations). |
EEXIST | The _O_CREAT and _O_EXCL flags are specified, but the named file already exists. |
EINVAL | An invalid oflag or pmode argument was given. |
EMFILE | No more file handles available (too many open files). |
ENOENT | File or path name not found. |
Standards:UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
Use _open for compatibility with ANSI naming conventions of non-ANSI functions. Use open and link with OLDNAMES.LIB for UNIX compatibility.
_access, _chmod, _close, _creat, _dup, _dup2, fopen, _sopen, _umask
/* OPEN.C: This program uses _open to open a file named OPEN.C for input
* and a file named OPEN.OUT for output. The files are then closed.
*/
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <io.h>
#include <stdio.h>
void main( void )
{
int fh1, fh2;
fh1 = _open( "OPEN.C", _O_RDONLY );
if( fh1 == -1 )
perror( "open failed on input file" );
else
{
printf( "open succeeded on input file\n" );
_close( fh1 );
}
fh2 = _open( "OPEN.OUT", _O_WRONLY | _O_CREAT, _S_IREAD | _S_IWRITE );
if( fh2 == -1 )
perror( "open failed on output file" );
else
{
printf( "open succeeded on output file\n" );
_close( fh2 );
}
}
open succeeded on input file
open succeeded on output file