_open, _dos_open
Opens a file for reading or writing.
Defined in header <fcntl.h>
Syntax
#include <fcntl.h>
int _open(const char *filename, int oflags);
#include <fcntl.h>
#include <share.h>
#include <dos.h>
unsigned _dos_open(const char *filename, unsigned oflags, int*handlep);
_open and _dos_open open the file specified by filename,then prepares it
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ |
Remarks
^^^ reading or writing, as determined by the value of oflags. The file is always opened in binary mode. The file handle is stored at the location pointed to by handlep.
oflags must include one of the following values:
| 0_RDONLY | Open for reading. |
| 0_WRONLY | Open for writing. |
| ORDWR | Open for reading and writing. |
On DOS 3.0 or later, the following additional values can be included in oflags (using an OR operation):
These symbolic
| 0_NOINHERIT | The file is not passed to child programs. |
constants are defined in fcntl.t)
| SH_COMPAT | Allow other opens with SH_COMPAT. | The call will fail |
and stiare.ti.
if the file has already been opened in any other shared mode.
| SH_DENYRW | Only the current handle may have access to the file. |
| SH_DENYWR | Allow only reads from any other open to the file. |
| SH_DENYRD | Allow only writes from any other open to the file. |
| SH DENYNO | Allow other shared opens to the file, but not other |
| SH_COMPAT | opens. |
| Only one of the SHDENYxx | values can be included in a single |
_dos_open or _open under DOS 3.0 or later. These file-sharing attributes are in addition to any locking performed on the files.
The maximum number of simultaneously open files is defined by HANDLE_MAX.
Return value
On successful completion, _open returns a nonnegative integer (the file handle). On successful completion, _dos_open returns 0, and stores the file handle at the location pointed to by handlep. The file pointer, which marks the current position in the file, is set to the beginning of the file.
On error, _open returns -1 and _dos_open returns the DOS error code. For both functions, the global variable errno is set to one of the following:
| ENOENT | Path or file not found |
| EMFILE | Too many open files |
| EACCES | Permission denied |
| EINVACC | Invalid access code |
See also
Example
#include <string.h>
iinclude <stdio.h>
#include <fcntl.h> •
#include <io.h>
int main(void) /* Example for _open. */
{
int handle;
char msg[] = "Hello world\n";
if ((handle = _open("TEST.$$$", 0_RDWR) -1) {
perrorC Error: ") ;
return 1;
_v;rite(handle, msg, strlen(msg)
_close (handle);
return 0;
[include <string.h>
[include <stdio.h>
#include <fcntl.h>
(include <dos.h>
int main (void) /* Example for _dos_open. */
{
int handle;
unsigned nbytes;
char msg[] = "Hello world\n";
if (_dos_open("TEST.$$$", 0_RDWR, &handle) != 0) {
perror("Unable to open TEST.$$$"),•
return 1;
if (_dos_write(handle, msg, strlen(msg),&nbytes)
perror( "Unable to write to TEST.$$$"),•
printf("%u bytes written to TEST.$$$\n",nbytes) ;
_dos_close (handle);
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Extraction notes
- heading `_open, _Glos_open` read as `_open, _dos_open`
Stable link: /3.1/fcntl.h/_open/
· short form /3.1/_open/