_open、_wopen_open, _wopen

打开文件。Opens a file. 已弃用这些函数,因为提供了更安全的版本;请参阅 _sopen_s、_wsopen_sThese functions are deprecated because more-secure versions are available; see _sopen_s, _wsopen_s.

语法Syntax

int _open(
   const char *filename,
   int oflag [,
   int pmode]
);
int _wopen(
   const wchar_t *filename,
   int oflag [,
   int pmode]
);

参数Parameters

filenamefilename
文件名。File name.

oflagoflag
允许的操作类型。The kind of operations allowed.

pmodepmode
权限模式。Permission mode.

返回值Return Value

其中每个函数都将为打开的文件返回文件描述符。Each of these functions returns a file descriptor for the opened file. 返回值-1 指示错误;在这种情况下, errno设置为以下值之一。A return value of -1 indicates an error; in that case errno is set to one of the following values.

errno 值errno value 条件Condition
EACCESEACCES 尝试打开只读文件进行写入,文件的共享模式不允许指定的操作,或给定路径是目录。Tried to open a read-only file for writing, file's sharing mode does not allow the specified operations, or the given path is a directory.
EEXISTEEXIST 已指定 _O_CREAT_O_EXCL标志,但filename已经存在。_O_CREAT and _O_EXCL flags specified, but filename already exists.
EINVALEINVAL 无效的oflagpmode参数。Invalid oflag or pmode argument.
EMFILEEMFILE 无法使用更多的文件描述符(打开的文件太多)。No more file descriptors are available (too many files are open).
ENOENTENOENT 未找到文件或路径。File or path not found.

有关这些代码及其他返回代码的详细信息,请参阅 errno、_doserrno、_sys_errlist 和 _sys_nerrFor more information about these and other return codes, see errno, _doserrno, _sys_errlist, and _sys_nerr.

备注Remarks

_Open函数打开文件名指定的文件,并根据oflag的指定使其做好读写准备。The _open function opens the file specified by filename and prepares it for reading or writing, as specified by oflag. _wopen_open的宽字符版本; _wopenfilename参数是宽字符字符串。_wopen is a wide-character version of _open; the filename argument to _wopen is a wide-character string. 否则, _wopen_open的行为相同。_wopen and _open behave identically otherwise.

一般文本例程映射Generic-Text Routine Mappings

Tchar.h 例程Tchar.h routine 未定义 _UNICODE 和 _MBCS_UNICODE and _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_topen_topen _open_open _open_open _wopen_wopen

oflag是一个由以下一个或多个清单常量或常量组合构成的整数表达式,在 fcntl.h> > 中<定义。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常量oflag constant 行为Behavior
_O_APPEND_O_APPEND 在执行每个写入操作之前,将文件指针移动到文件末尾。Moves the file pointer to the end of the file before every write operation.
_O_BINARY_O_BINARY 在二进制(未转换)模式下打开该文件。Opens the file in binary (untranslated) mode. (有关二进制模式的说明,请参阅 fopen。)(See fopen for a description of binary mode.)
_O_CREAT_O_CREAT 创建文件并打开它以供写入。Creates a file and opens it for writing. 如果文件名指定的文件存在,则不起作用。Has no effect if the file specified by filename exists. 指定 _O_CREAT时, pmode参数是必需的。The pmode argument is required when _O_CREAT is specified.
_O_CREAT | _O_SHORT_LIVED_O_CREAT | _O_SHORT_LIVED 创建一个文件作为临时文件,如果可能,请不要将它刷新到磁盘中。Creates a file as temporary and if possible does not flush to disk. 指定 _O_CREAT时, pmode参数是必需的。The pmode argument is required when _O_CREAT is specified.
_O_CREAT | _O_TEMPORARY_O_CREAT | _O_TEMPORARY 创建一个文件作为临时文件;在关闭最后一个文件描述符时,删除该文件。Creates a file as temporary; the file is deleted when the last file descriptor is closed. 指定 _O_CREAT时, pmode参数是必需的。The pmode argument is required when _O_CREAT is specified.
_O_CREAT | _O_EXCL_O_CREAT | _O_EXCL 如果文件名指定的文件存在,则返回一个错误值。Returns an error value if a file specified by filename exists. 仅当与 _O_CREAT一起使用时才适用。Applies only when used with _O_CREAT.
_O_NOINHERIT_O_NOINHERIT 阻止创建共享文件描述符。Prevents creation of a shared file descriptor.
_O_RANDOM_O_RANDOM 指定缓存针对(但不限于)从磁盘的随机访问进行优化。Specifies that caching is optimized for, but not restricted to, random access from disk.
_O_RDONLY_O_RDONLY 打开文件以供只读。Opens a file for reading only. 不能与 _O_RDWR_O_WRONLY一起指定。Cannot be specified with _O_RDWR or _O_WRONLY.
_O_RDWR_O_RDWR 打开文件以供读取和写入。Opens a file for both reading and writing. 不能与 _O_RDONLY_O_WRONLY一起指定。Cannot be specified with _O_RDONLY or _O_WRONLY.
_O_SEQUENTIAL_O_SEQUENTIAL 指定缓存针对(但不限于)从磁盘的顺序访问进行优化。Specifies that caching is optimized for, but not restricted to, sequential access from disk.
_O_TEXT_O_TEXT 在文本(转换)模式下打开文件。Opens a file in text (translated) mode. (有关详细信息,请参阅文本和二进制模式文件 I/Ofopen。)(For more information, see Text and Binary Mode File I/O and fopen.)
_O_TRUNC_O_TRUNC 打开文件并将其长度截断为零;该文件必须具有写入权限。Opens a file and truncates it to zero length; the file must have write permission. 不能与 _O_RDONLY一起指定。Cannot be specified with _O_RDONLY. 用于 _O_CREAT_O_TRUNC打开现有文件或创建文件。_O_TRUNC used with _O_CREAT opens an existing file or creates a file. 注意: _O_TRUNC标志会销毁指定文件的内容。Note: The _O_TRUNC flag destroys the contents of the specified file.
_O_WRONLY_O_WRONLY 打开文件以供只写。Opens a file for writing only. 不能与 _O_RDONLY_O_RDWR一起指定。Cannot be specified with _O_RDONLY or _O_RDWR.
_O_U16TEXT_O_U16TEXT 在 Unicode UTF-16 模式下打开文件。Opens a file in Unicode UTF-16 mode.
_O_U8TEXT_O_U8TEXT 在 Unicode UTF-8 模式下打开文件。Opens a file in Unicode UTF-8 mode.
_O_WTEXT_O_WTEXT 在 Unicode 模式下打开文件。Opens a file in Unicode mode.

若要指定文件访问模式,必须指定 _O_RDONLY_O_RDWR_O_WRONLYTo 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.

如果 _O_WTEXT用于打开要读取的文件,则 _open将读取该文件的开头并检查字节顺序标记(BOM)。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). 如果存在 BOM,则将该文件视为 UTF-8 或 UTF-16LE,具体取决于该 BOM。If there is a BOM, the file is treated as UTF-8 or UTF-16LE, depending on the BOM. 如果不存在 BOM,则将该文件视为 ANSI。If no BOM is present, the file is treated as ANSI. 使用 _O_WTEXT打开文件进行写入时,将使用 utf-16。When a file is opened for writing by using _O_WTEXT, UTF-16 is used. 无论前面的设置或字节顺序标记如何,如果使用 _O_U8TEXT ,文件始终以 utf-8 形式打开;如果使用了 _O_U16TEXT ,则文件始终以 utf-16 的形式打开。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.

使用 _O_WTEXT_O_U8TEXT_O_U16TEXT在 Unicode 模式下打开文件时,输入函数会将从文件读取的数据转换为存储为类型wchar_t的 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. 写入在 Unicode 模式下打开的文件的函数需要包含存储为wchar_t类型的 utf-16 数据的缓冲区。Functions that write to a file opened in Unicode mode expect buffers that contain UTF-16 data stored as type wchar_t. 如果将文件编码为 UTF-8,则在写入它时,UTF-16 数据会转换为 UTF-8;在读取它时,该文件的 UTF-8 编码的内容会转换为 UTF-16。If the file is encoded as UTF-8, then UTF-16 data is translated into UTF-8 when it is written, and the file's UTF-8-encoded content is translated into UTF-16 when it is read. 尝试在 Unicode 模式下读取或写入奇数个字节会导致参数验证错误。An attempt to read or write an odd number of bytes in Unicode mode causes a parameter validation error. 若要读取或写入在你的程序中存储为 UTF-8 的数据,请使用文本或二进制文件模式,而不是 Unicode 模式。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 are responsible for any required encoding translation.

如果 _open是通过 _O_WRONLY | _O_APPEND (追加模式)和 _O_WTEXT_O_U16TEXT_O_U8TEXT调用的,则它首先会尝试打开文件以进行读取和写入、读取 BOM,然后将其重新打开仅限写入。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. 如果无法打开该文件以供读取和写入,则它将打开该文件以供只写,并使用 Unicode 模式设置的默认值。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.

当使用两个或多个清单常量构成oflag参数时,这些常量将与按位 "或" 运算符( | )组合在一起。When two or more manifest constants are used to form the oflag argument, the constants are combined with the bitwise-OR operator ( | ). 有关二进制模式和文本模式的讨论,请参阅文本和二进制模式文件 I/OFor a discussion of binary and text modes, see Text and Binary Mode File I/O.

仅当指定 _O_CREAT时, pmode参数是必需的。The pmode argument is required only when _O_CREAT is specified. 如果文件已存在,则会忽略pmodeIf the file already exists, pmode is ignored. 否则, pmode将指定在首次关闭新文件时设置的文件权限设置。Otherwise, pmode specifies the file permission settings, which are set when the new file is closed the first time. 在设置权限之前, _open会将当前文件权限掩码应用到pmode_open applies the current file-permission mask to pmode before the permissions are set. (有关详细信息,请参阅_umask。)pmode是一个整数表达式,它包含在 sys\stat.h > 中<定义的以下一个或两个清单常量。(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>.

pmodepmode 含义Meaning
_S_IREAD_S_IREAD 只允许读取。Only reading permitted.
_S_IWRITE_S_IWRITE 允许写入。Writing permitted. (实际上,允许读取和写入。)(In effect, permits reading and writing.)
_S_IREAD | _S_IWRITE_S_IREAD | _S_IWRITE 允许读取和写入。Reading and writing permitted.

当提供两个常量时,它们将与按位 "或" 运算符 | ()联接。When both constants are given, they are joined with the bitwise-OR operator ( | ). 在 Windows 中,所有文件均可读;不会提供只写权限。In Windows, all files are readable; write-only permission is not available. 因此, _S_IWRITE_S_IREAD | _S_IWRITE模式是等效的。Therefore, the modes _S_IWRITE and _S_IREAD | _S_IWRITE are equivalent.

如果为pmode指定了 _S_IREAD_S_IWRITE的某个组合以外的值,即使它会在另一个操作系统中指定有效的pmode ,或除允许的oflag值以外的任何值也是如此。指定,函数将在调试模式下生成断言并调用无效参数处理程序,如参数验证中所述。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. 如果允许执行继续,则该函数将返回-1,并将errno设置为EINVALIf execution is allowed to continue, the function returns -1 and sets errno to EINVAL.

要求Requirements

例程所返回的值Routine 必需的标头Required header 可选标头Optional header
_open_open <io.h><io.h> <fcntl.h>、<sys\types.h>、<sys\stat.h><fcntl.h>, <sys\types.h>, <sys\stat.h>
_wopen_wopen <io.h> 或 <wchar.h><io.h> or <wchar.h> <fcntl.h>、<sys\types.h>、<sys\stat.h><fcntl.h>, <sys\types.h>, <sys\stat.h>

_open_wopen是 Microsoft 扩展。_open and _wopen are Microsoft extensions. 有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.

Libraries

C 运行时库的所有版本。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>

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 );
   }
}

OutputOutput

Open succeeded on input file
Open succeeded on output file

请参阅See also

低级别 I/OLow-Level I/O
_chmod、_wchmod_chmod, _wchmod
_close_close
_creat、_wcreat_creat, _wcreat
_dup、_dup2_dup, _dup2
fopen、_wfopen_wfopenfopen, _wfopen
_sopen、_wsopen_sopen, _wsopen