Skip to content

Latest commit

 

History

History
181 lines (141 loc) · 9.98 KB

open-wopen.md

File metadata and controls

181 lines (141 loc) · 9.98 KB
descriptiontitlems.dateapi_nameapi_locationapi_typetopic_typef1_keywordshelpviewer_keywordsms.assetid
Learn more about: _open, _wopen
_open, _wopen
05/18/2022
_open
_wopen
msvcrt.dll
msvcr80.dll
msvcr90.dll
msvcr100.dll
msvcr100_clr0400.dll
msvcr110.dll
msvcr110_clr0400.dll
msvcr120.dll
msvcr120_clr0400.dll
ucrtbase.dll
api-ms-win-crt-stdio-l1-1-0.dll
DLLExport
apiref
CORECRT_IO/_open
CORECRT_WIO/_wopen
TCHAR/_topen
_open
_wopen
_topen
opening files, for file I/O
topen function
_open function
_topen function
_wopen function
files [C++], opening
wopen function
open function
13f6a0c3-d1aa-450d-a7aa-74abc91b163e

_open, _wopen

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

Syntax

int_open( constchar*filename, intoflag [, intpmode] ); int_wopen( constwchar_t*filename, intoflag [, intpmode] );

Parameters

filename
File name.

oflag
The kind of operations allowed.

pmode
Permission mode.

Return value

Each of these functions returns a file descriptor for the opened file. A return value of -1 indicates an error; in that case errno is set to one of the following values.

errno valueCondition
EACCESTried 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, but filename already exists.
EINVALInvalid oflag or pmode argument.
EMFILENo more file descriptors are available (too many files are open).
ENOENTFile or path not found.

For more information about these and other return codes, see errno, _doserrno, _sys_errlist, and _sys_nerr.

Remarks

The _open function opens the file specified by filename and prepares it 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.

Generic-text routine mappings

<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 constantBehavior
_O_APPENDMoves the file pointer to the end of the file before every write operation.
_O_BINARYOpens the file in binary (untranslated) mode. (See fopen for a description of binary mode.)
_O_CREATCreates a file and opens it for writing. Has no effect if the file specified by filename exists. The pmode argument is required when _O_CREAT is specified.
**`_O_CREAT_O_SHORT_LIVED`**
**`_O_CREAT_O_TEMPORARY`**
**`_O_CREAT_O_EXCL`**
_O_NOINHERITPrevents creation of a shared file descriptor.
_O_RANDOMSpecifies that caching is optimized for, but not restricted to, random access from disk.
_O_RDONLYOpens a file for reading only. Can't be specified with _O_RDWR or _O_WRONLY.
_O_RDWROpens a file for both reading and writing. Can't be specified with _O_RDONLY or _O_WRONLY.
_O_SEQUENTIALSpecifies that caching is optimized for, but not restricted to, sequential access from disk.
_O_TEXTOpens a file in ANSI text (translated) mode. (For more information, see Text and binary mode file I/O and fopen.)
_O_TRUNCOpens 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_WRONLYOpens a file for writing only. Can't be specified with _O_RDONLY or _O_RDWR.
_O_U16TEXTOpens a file in Unicode UTF-16 mode.
_O_U8TEXTOpens a file in Unicode UTF-8 mode.
_O_WTEXTOpens 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 type wchar_t. Functions that write to a file opened in Unicode mode expect buffers that contain UTF-16 data stored as type wchar_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 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 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>.

pmodeMeaning
_S_IREADOnly reading permitted.
_S_IWRITEWriting permitted. (In effect, permits reading and writing.)
**`_S_IREAD_S_IWRITE`**

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 for pmode—even if it would specify a valid pmode in another operating system—or if 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

FunctionRequired headerOptional 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, see Compatibility.

Libraries

All versions of the C run-time libraries.

Example

// 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>intmain( void ) { intfh1, fh2; fh1=_open( "CRT_OPEN.C", _O_RDONLY ); // C4996// Note: _open is deprecated; consider using _sopen_s insteadif( 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 ); // C4996if( fh2==-1 ) perror( "Open failed on output file" ); else { printf( "Open succeeded on output file\n" ); _close( fh2 ); } }

Output

Open succeeded on input file Open succeeded on output file 

See also

Low-level I/O
_chmod, _wchmod
_close
_creat, _wcreat
_dup, _dup2
fopen, _wfopen
_sopen, _wsopen

close