_open, _wopen
Opens 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] );
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.
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.
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.
If _open is called with _O_WRONLY|_O_APPEND (append mode) and _O_WTEXT, _O_U16TEXT, or _O_U8TEXT, it will first try 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 will open the file for writing only and use the default value for the Unicode mode setting.
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.
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.
Routine | 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> |
For more compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C 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 );
}
}
Note