Opens a file. These functions are deprecated because more secure versions are available; see _sopen_s, _wsopen_s.


Parameters
-
filename
-
File name.
-
oflag
-
Type of operations allowed.
-
pmode
-
Permission mode.

Each of these functions returns a file descriptor for the opened file. A return value of -1 indicates an error, in which case errno is set to one of the following values.
-
EACCES
-
Tried to open read-only file for writing, file's sharing mode does not allow specified operations, or given path is directory.
-
EEXIST
-
_O_CREAT and _O_EXCL flags specified, but filename already exists.
-
EINVAL
-
Invalid oflag or pmode argument.
-
EMFILE
-
No more file descriptors available (too many open files).
-
ENOENT
-
File or path not found.
For more information about these and other return codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

The _open function opens the file specified by filename and prepares the file for reading or writing, as specified by oflag. _wopen is a wide-character version of _open; the filename argument to _wopen is a wide-character string. _wopen and _open behave identically otherwise.
Tchar.h routine | _UNICODE and _MBCS not defined | _MBCS defined | _UNICODE defined |
---|---|---|---|
_topen | _open | _open | _wopen |
oflag is an integer expression formed from one or more of the following manifest constants or constant combinations defined in Fcntl.h.
-
_O_APPEND
-
Moves file pointer to end of file before every write operation.
-
_O_BINARY
-
Opens file in binary (untranslated) mode. (See fopen for a description of binary mode.)
-
_O_CREAT
-
Creates and opens a new file for writing. Has no effect if the file specified by filename exists. pmode argument is required when _O_CREAT is specified.
-
_O_CREAT|
_O_SHORT_LIVED
-
Create a file as temporary and if possible do not flush to disk. pmode argument is required when _O_CREAT is specified.
-
_O_CREAT|
_O_TEMPORARY
-
Create a file as temporary; the file is deleted when the last file descriptor is closed. pmode argument is required when _O_CREAT is specified.
-
_O_CREAT|
_O_EXCL
-
Returns an error value if the file specified by filename exists. Applies only when used with _O_CREAT.
-
_O_NOINHERIT
-
Prevents creation of a shared file descriptor.
-
_O_RANDOM
-
Specifies that caching is optimized for, but not restricted to, random access from disk.
-
_O_RDONLY
-
Opens a file for reading only; cannot be specified with _O_RDWR or _O_WRONLY.
-
_O_RDWR
-
Opens file for both reading and writing; you cannot specify this flag with _O_RDONLY or _O_WRONLY.
-
_O_SEQUENTIAL
-
Specifies that caching is optimized for, but not restricted to, sequential access from disk.
-
_O_TEXT
-
Opens a file in text (translated) mode. (For more information, see Text and Binary Mode File I/O and fopen.)
-
_O_TRUNC
-
Opens a file and truncates it to zero length; the file must have write permission. You cannot specify this flag with _O_RDONLY. _O_TRUNC used with _O_CREAT opens an existing file or creates a new file.
Note
The _O_TRUNC flag destroys the contents of the specified file.
-
_O_U16TEXT
-
Open the file in Unicode UTF-16 mode. This option is available in Visual C++ 2005.
-
_O_U8TEXT
-
Open the file in Unicode UTF-8 mode. This option is available in Visual C++ 2005.
-
_O_WTEXT
-
Open the file in Unicode mode. This option is available in Visual C++ 2005.
-
_O_WRONLY
-
Opens the file for writing only; cannot be specified with _O_RDONLY or _O_RDWR.
To specify the file access mode, you must specify either _O_RDONLY, _O_RDWR, or _O_WRONLY. There is no default value for the access mode.
If _O_WTEXT is used to open a file for reading, _open reads the beginning of the file and check for a byte order mark (BOM). If there is a BOM, the file is treated as UTF-8 or UTF-16LE depending on the BOM. If no BOM is present, the file is treated as ANSI. When a file is opened for writing using _O_WTEXT, UTF-16 is used. If _O_UTF8 is used, the file is always opened as UTF-8 and if _O_UTF16 is used, the file is always opened as UTF-16 regardless of any previous setting or byte order mark.
When two or more manifest constants are used to form the oflag argument, the constants are combined with the bitwise-OR operator ( | ). For a discussion of binary and text modes, see Text and Binary Mode File I/O.
The pmode argument is required only when _O_CREAT is specified. If the file already exists, pmode is ignored. Otherwise, pmode specifies the file permission settings, which are set when the new file is closed the first time. _open applies the current file-permission mask to pmode before setting the permissions (for more information, see _umask). pmode is an integer expression containing one or both of the following manifest constants, defined in SYS/Stat.h.
-
_S_IREAD
-
Reading only permitted.
-
_S_IWRITE
-
Writing permitted (effectively permits reading and writing).
-
_S_IREAD | _S_IWRITE
-
Reading and writing permitted.
When both constants are given, they are joined with the bitwise-OR operator ( | ). In Windows NT, all files are readable, so write-only permission is not available; thus the modes _S_IWRITE and _S_IREAD | _S_IWRITE are equivalent.
If a value other than the above is specified for pmode (even if it would specify a valid pmode in another operating system) or any value other than the allowed oflag values is specified, the function generates an assertion in Debug mode and invokes the invalid parameter handler as described in Parameter Validation. If execution is allowed to continue, the function returns -1 and sets errno to EINVAL.
Requirements
Routine | Required header | Optional header | Compatibility |
---|---|---|---|
_open | <io.h> | <fcntl.h>, <sys/types.h>, <sys/stat.h> | Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 |
_wopen | <io.h> or <wchar.h> | <fcntl.h>, <sys/types.h>, <sys/stat.h> | Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 |
For more compatibility information, see Compatibility in the Introduction.

All versions of the C run-time libraries.







































