_sopen、_wsopen_sopen, _wsopen

打开文件以供共享。Opens a file for sharing. 提供这些函数的更多安全版本:请参阅_sopen_s,_wsopen_sMore secure versions of these functions are available: see _sopen_s, _wsopen_s.

语法Syntax

int _sopen(
   const char *filename,
   int oflag,
   int shflag [,
   int pmode ]
);
int _wsopen(
   const wchar_t *filename,
   int oflag,
   int shflag [,
   int pmode ]
);

参数Parameters

filenamefilename
文件名。File name.

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

shflagshflag
允许的共享类型。The kind of sharing allowed.

pmodepmode
权限设置。Permission setting.

返回值Return Value

其中每个函数都将为打开的文件返回文件描述符。Each of these functions returns a file descriptor for the opened file.

如果filenameoflagNULL指针,或oflagshflag不在有效的值范围内,则会调用无效参数处理程序,如参数验证中所述。If filename or oflag is a NULL pointer, or if oflag or shflag is not within a valid range of values, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则这些函数将返回-1,并将errno设置为以下值之一。If execution is allowed to continue, these functions return -1 and set errno to one of the following values.

errno 值errno value 条件Condition
EACCESEACCES 给定路径是目录,或者文件是只读的,但是已尝试打开以供写入操作。The given path is a directory, or the file is read-only, but an open-for-writing operation was attempted.
EEXISTEEXIST 已指定 _O_CREAT_O_EXCL标志,但filename已经存在。_O_CREAT and _O_EXCL flags were specified, but filename already exists.
EINVALEINVAL 无效的oflagshflag参数。Invalid oflag or shflag argument.
EMFILEEMFILE 没有更多可用的文件描述符。No more file descriptors are available.
ENOENTENOENT 未找到文件或路径。File or path is not found.

有关这些属性和其他的更多信息返回代码示例,请参见 _doserrno、errno、_sys_errlist 和 _sys_nerrFor more information about these and other return codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

备注Remarks

_Sopen函数打开文件名指定的文件,并根据oflagshflag的定义,准备文件以供共享读取或写入。The _sopen function opens the file specified by filename and prepares the file for shared reading or writing, as defined by oflag and shflag. _wsopen_sopen的宽字符版本; _wsopenfilename参数是宽字符字符串。_wsopen is a wide-character version of _sopen; the filename argument to _wsopen is a wide-character string. 否则, _wsopen_sopen的行为相同。_wsopen and _sopen 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
_tsopen_tsopen _sopen_sopen _sopen_sopen _wsopen_wsopen

整数表达式oflag是通过合并在 fcntl.h> > 中<定义的以下一个或多个清单常量而形成的。The integer expression oflag is formed by combining one or more of the following manifest constants, which are defined in <fcntl.h>. 当两个或多个常量构成参数oflag时,它们将与按位 "或" | 运算符()组合在一起。When two or more constants form the argument oflag, they are combined with the bitwise-OR operator ( | ).

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_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.

如果 _sopen是通过 _O_WRONLY | _O_APPEND (追加模式)和 _O_WTEXT_O_U16TEXT_O_U8TEXT调用的,则它首先会尝试打开文件以进行读取和写入、读取 BOM,然后将其重新打开仅限写入。If _sopen 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.

自变量shflag是一个常量表达式,它包含在 share > 中<定义的以下清单常量之一。The argument shflag is a constant expression consisting of one of the following manifest constants, which are defined in <share.h>.

shflag常量shflag constant 行为Behavior
_SH_DENYRW_SH_DENYRW 拒绝对文件的读取和写入访问。Denies read and write access to a file.
_SH_DENYWR_SH_DENYWR 拒绝对文件的写入访问。Denies write access to a file.
_SH_DENYRD_SH_DENYRD 拒绝对文件的读取访问。Denies read access to a file.
_SH_DENYNO_SH_DENYNO 允许读取和写入访问。Permits read and write access.

仅当指定 _O_CREAT时, pmode参数是必需的。The pmode argument is required only when _O_CREAT is specified. 如果文件不存在,则pmode指定文件的权限设置,该设置是在首次关闭新文件时设置的。If the file does not exist, pmode specifies the file's permission settings, which are set when the new file is closed the first time. 否则,将忽略pmodeOtherwise, pmode is ignored. pmode是一个整数表达式,它包含 sys\stat.h > 中<定义的清单常量 _S_IWRITE_S_IREADpmode is an integer expression that contains one or both of the manifest constants _S_IWRITE and _S_IREAD, which are defined in <sys\stat.h>. 当给定这两个常量时,将使用按位“或”运算符合并它们。When both constants are given, they are combined with the bitwise-OR operator. Pmode的含义如下所示。The meaning of pmode is as follows.

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.

如果未授予写入权限,则该文件为只读。If write permission is not given, the file is read-only. 在 Windows 操作系统中,所有文件均可读;不可能提供只写权限。In the Windows operating system, all files are readable; it is not possible to give write-only permission. 因此, _S_IWRITE_S_IREAD | _S_IWRITE模式是等效的。Therefore, the modes _S_IWRITE and _S_IREAD | _S_IWRITE are equivalent.

在设置权限之前, _sopen会将当前文件权限掩码应用到pmode_sopen applies the current file-permission mask to pmode before the permissions are set. (请参阅 _umask。)(See _umask.)

要求Requirements

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

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.

示例Example

请参阅 _locking 的示例。See the example for _locking.

请参阅See also

低级别 I/OLow-Level I/O
_close_close
_creat、_wcreat_creat, _wcreat
fopen、_wfopen_wfopenfopen, _wfopen
_fsopen、_wfsopen_fsopen, _wfsopen
_open、_wopen_open, _wopen