open
Opens a file for reading or writing.
Defined in header <fcntl.h>
Syntax
#include <fcntl.h>
#include <sys\stat.h>
int open(const char *path, int access [,unsigned mode]);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ | ■ |
Remarks
open opens the file specified by path, then prepares it for reading and/or writing as determined by the value of access.
To create a file in a particular mode, you can either assign to the global
| variable _fmode or call open with the 0_CREAT | and 0_TRUNC | options |
ORed with the translation mode desired. For example, the call
open("xmp",0_CREATI0_TRUNCI0_BINARY,S_IREAD) will create a binary-mode, read-only file named XMP, truncating its length to 0 bytes if it already existed.
For open, access is constructed by bitwise ORing flags from the following two lists. Only one flag from the first list can be used (and one must be used); the remaining flags can be used in any logical combination.
List 1: Read/write flags
These symbolic
| 0_RDONLY | Open for reading only. |
constants are
| 0_WRONLY | Open for writing only. |
defined in fcntl.ti.
| 0_RDWR | Open for reading and writing. |
List 2: Other access flags
0_NDELAY Not used; for UNIX compatibility. 0_APPEND If set, the file pointer will be set to the end of the file prior to each write.
| 0_CREAT | If the file exists, this flag has no effect. If the file does not exist, the file is created, and the bits of mode are used to set the file attribute bits as in chmod. |
0_TRUNC If the file exists, its length is truncated to 0. The file attributes remain unchanged.
| 0_EXCL | Used only with 0_CREAT. error is returned. | If the file already exists, an |
OBINARY Can be given to explicitly open the file in binary mode.
| O_TEXT | Can be given to explicitly open the file in text mode. |
| If neither O_BINARY | nor O_TEXT is given, the file is opened in the |
translation mode set by the global variable Jrnode.
| If the 0_CREAT | flag is used in constructing access, you need to supply the |
mode argument to open from the following symbolic constants defined in sysXstat.h.
| Value of mode | Access permission |
| S_IWRITE | Permission to write |
| S IREAD | Permission to read |
| SJREAD 1SJWRITE | Permission to read and write |
Return value
On successful completion, open returns a nonnegative integer (the file handle). 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 the global variable errno is set to one of the following:
ENOENT No such file or directory EMFILE Too many open files
| EACCES | Permission denied |
| EINVACC | Invalid access code |
See also
chmodchsizeclosecreatcreatcreatnewcreattempdupdup2fdopenfilelengthfopenfreopengetftimelseeklockopenreadsopenwritewrite
Example
#include <string.h>
#include <stdio.h>
*include <fcntl.h>
#include <io.h>
int main (void)
{
int handle;
char msg[] = "Hello world";
if ((handle = open("TEST.$$$" , 0_CREAT I 0_TEXT)) == -1) {
perror ("Error:");
return 1;
}■
write(handle, msg, strlen(msg));
close(handle) ;
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/fcntl.h/open/
· short form /3.1/open/