_sopen、_wsopen_sopen, _wsopen
共有用にファイルを開きます。Opens a file for sharing. これらの関数のセキュリティを強化したバージョンについては 、「_sopen_s、_wsopen_s」を参照してください。More 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.
Filename または oflag が NULL ポインターの場合、または oflag または shflag が有効な値の範囲内にない場合は、「パラメーターの検証」で説明されているように、無効なパラメーターハンドラーが呼び出されます。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 | Oflag または shflag 引数が無効です。Invalid oflag or shflag argument. |
EMFILEEMFILE | ファイル記述子をこれ以上使用できません。No more file descriptors are available. |
ENOENTENOENT | ファイルまたはパスが見つかりません。File or path is not found. |
リターン コードの詳細については、「_doserrno、errno、_sys_errlist、および _sys_nerr」を参照してください。For more information about these and other return codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.
解説Remarks
_Sopen 関数は、 filename で指定されたファイルを開き、 oflag および shflag で定義されている共有の読み取りまたは書き込み用にファイルを準備します。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 のワイド文字バージョンです。_wsopen する filename 引数は、ワイド文字列です。_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.
既定では、この関数のグローバル状態はアプリケーションにスコープが設定されています。By default, this function's global state is scoped to the application. これを変更するには、「 CRT でのグローバル状態」を参照してください。To change this, see Global state in the CRT.
汎用テキスト ルーチンのマップ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 は、で定義されている次のマニフェスト定数の1つ以上を組み合わせることによって形成され <fcntl.h> ます。The integer expression oflag is formed by combining one or more of the following manifest constants, which are defined in <fcntl.h>. 2つ以上の定数が引数 oflag を形成する場合、それらはビットごとの or 演算子 ( | ) と組み合わされます。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. Filename によって指定されたファイルが存在する場合は効果がありません。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 |
Filename によって指定されたファイルが存在する場合、エラー値を返します。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. (詳細については、「テキスト モードとバイナリ モードのファイル入出力」および「fopen」を参照してください。)(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_WRONLY のいずれかを指定する必要があります。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.
_O_WTEXT、 _O_U8TEXT、または _O_U16TEXT を使用してファイルを Unicode モードで開くと、入力関数は、そのファイルから読み取られたデータを、型として格納された utf-16 データに変換し wchar_t
ます。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 モードで開かれたファイルに書き込む関数は、型として格納された UTF-16 データを含むバッファーを想定 wchar_t
します。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.
_O_WRONLY | _O_APPEND (追加モード) と _O_WTEXT、 _O_U16TEXT、または _O_U8TEXT を使用して _sopen が呼び出された場合、まず読み取りと書き込みのためにファイルを開き、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.h> 。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. |
Pmode 引数は _O_CREAT が指定されている場合にのみ必要です。The pmode argument is required only when _O_CREAT is specified. ファイルが存在しない場合は、ファイルのアクセス許可の設定 を指定し ます。これは、新しいファイルが最初に閉じられたときに設定されます。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. それ以外の場合、 pmode は無視されます。Otherwise, pmode is ignored. pmode は、で定義されているマニフェスト定数 _S_IWRITE と _S_IREAD の一方または両方を含む整数式です <sys\stat.h> 。pmode 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>. 両方の定数が指定されると、これらはビットごとの OR 演算子を使用して組み合わされます。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> | <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> |
互換性について詳しくは、「 Compatibility」をご覧ください。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、_wfopenfopen, _wfopen
_fsopen、_wfsopen_fsopen, _wfsopen
_open、_wopen_open, _wopen