This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
_open,_wopenOpens a file. These functions are deprecated because more-secure versions are available; see_sopen_s,_wsopen_s.
int _open( const char *filename, int oflag [, int pmode]);int _wopen( const wchar_t *filename, int oflag [, int pmode]);filename
File name.
oflag
The kind 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 that caseerrno is set to one of the following values.
errno value | Condition |
|---|---|
EACCES | Tried to open a read-only file for writing, file's sharing mode doesn't allow the specified operations, or the given path is a directory. |
EEXIST | _O_CREAT and_O_EXCL flags specified, butfilename already exists. |
EINVAL | Invalidoflag orpmode argument. |
EMFILE | No more file descriptors are available (too many files are open). |
ENOENT | File or path not found. |
For more information about these and other return codes, seeerrno,_doserrno,_sys_errlist, and_sys_nerr.
The_open function opens the file specified byfilename and prepares it for reading or writing, as specified byoflag._wopen is a wide-character version of_open; thefilename 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, which are defined in<fcntl.h>.
oflag constant | Behavior |
|---|---|
_O_APPEND | Moves the file pointer to the end of the file before every write operation. |
_O_BINARY | Opens the file in binary (untranslated) mode. (Seefopen for a description of binary mode.) |
_O_CREAT | Creates a file and opens it for writing. Has no effect if the file specified byfilename exists. Thepmode argument is required when_O_CREAT is specified. |
_O_CREAT | _O_SHORT_LIVED | Creates a file as temporary and if possible doesn't flush to disk. Thepmode argument is required when_O_CREAT is specified. |
_O_CREAT | _O_TEMPORARY | Creates a file as temporary; the file is deleted when the last file descriptor is closed. Thepmode argument is required when_O_CREAT is specified. To preserve legacy behavior for app-compatibility, other processes aren't prevented from deleting this file. |
_O_CREAT | _O_EXCL | Returns an error value if a file specified byfilename 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. Can't be specified with_O_RDWR or_O_WRONLY. |
_O_RDWR | Opens a file for both reading and writing. Can't be specified 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 ANSI text (translated) mode. For more information, seeText and binary mode file I/O andfopen. |
_O_TRUNC | Opens a file and truncates it to zero length; the file must have write permission. Can't be specified with_O_RDONLY._O_TRUNC used with_O_CREAT opens an existing file or creates a file.Note: The_O_TRUNC flag destroys the contents of the specified file. |
_O_WRONLY | Opens a file for writing only. Can't be specified with_O_RDONLY or_O_RDWR. |
_O_U16TEXT | Opens a file in Unicode UTF-16 mode. |
_O_U8TEXT | Opens a file in Unicode UTF-8 mode. |
_O_WTEXT | Opens a file in Unicode mode. |
To specify the file access mode, you must specify either_O_RDONLY,_O_RDWR, or_O_WRONLY. There's 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 checks for a byte order mark (BOM). If there's 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 by using_O_WTEXT, UTF-16 is used. Regardless of any previous setting or byte order mark, if_O_U8TEXT is used, the file is always opened as UTF-8; if_O_U16TEXT is used, the file is always opened as UTF-16.
When a file is opened in Unicode mode by using_O_WTEXT,_O_U8TEXT, or_O_U16TEXT, input functions translate the data that's read from the file into UTF-16 data stored as typewchar_t. Functions that write to a file opened in Unicode mode expect buffers that contain UTF-16 data stored as typewchar_t. If the file is encoded as UTF-8, then UTF-16 data is translated into UTF-8 when it's written. The file's UTF-8-encoded content is translated into UTF-16 when it's read. An attempt to read or write an odd number of bytes in Unicode mode causes a parameter validation error. To read or write data that's stored in your program as UTF-8, use a text or binary file mode instead of a Unicode mode. You're responsible for any required encoding translation.
If_open is called with_O_WRONLY | _O_APPEND (append mode) and_O_WTEXT,_O_U16TEXT, or_O_U8TEXT, it first tries to open the file for reading and writing, read the BOM, then reopen it for writing only. If opening the file for reading and writing fails, it opens the file for writing only and uses the default value for the Unicode mode setting.
When two or more manifest constants are used to form theoflag argument, the constants are combined with the bitwise-OR operator (| ). For a discussion of binary and text modes, seeText and binary mode file I/O.
Thepmode 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 topmode before the permissions are set. For more information, see_umask.pmode is an integer expression that contains one or both of the following manifest constants, which are defined in<sys\stat.h>.
pmode | Meaning |
|---|---|
_S_IREAD | Only reading permitted. |
_S_IWRITE | Writing permitted. (In effect, permits reading and writing.) |
_S_IREAD | _S_IWRITE | Reading and writing permitted. |
When both constants are given, they're joined with the bitwise-OR operator (| ). In Windows, all files are readable; write-only permission isn't available. Therefore, the modes_S_IWRITE and_S_IREAD |_S_IWRITE are equivalent.
If a value other than some combination of_S_IREAD and_S_IWRITE is specified forpmode—even if it would specify a validpmode in another operating system—or if any value other than the allowedoflag values is specified, the function generates an assertion in Debug mode and invokes the invalid parameter handler, as described inParameter validation. If execution is allowed to continue, the function returns -1 and setserrno toEINVAL.
| Function | Required header | Optional header |
|---|---|---|
_open | <io.h> | <fcntl.h>,<sys\types.h>,<sys\stat.h> |
_wopen | <io.h> or<wchar.h> | <fcntl.h>,<sys\types.h>,<sys\stat.h> |
_open and_wopen are Microsoft extensions. For more compatibility information, seeCompatibility.
All versions of theC run-time libraries.
// crt_open.c// compile with: /W3/* This program uses _open to open a file* named CRT_OPEN.C for input and a file named CRT_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>int main( void ){ int fh1, fh2; fh1 = _open( "CRT_OPEN.C", _O_RDONLY ); // C4996 // Note: _open is deprecated; consider using _sopen_s instead if( fh1 == -1 ) perror( "Open failed on input file" ); else { printf( "Open succeeded on input file\n" ); _close( fh1 ); } fh2 = _open( "CRT_OPEN.OUT", _O_WRONLY | _O_CREAT, _S_IREAD | _S_IWRITE ); // C4996 if( fh2 == -1 ) perror( "Open failed on output file" ); else { printf( "Open succeeded on output file\n" ); _close( fh2 ); }}Open succeeded on input fileOpen succeeded on output fileLow-level I/O_chmod,_wchmod_close_creat,_wcreat_dup,_dup2fopen,_wfopen_sopen,_wsopen
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?