_creat、_wcreat_creat, _wcreat
新しいファイルを 1 つ作成します。Creates a new file. _creat と _wcreat は非推奨とされました。代わりに 、_sopen_s、_wsopen_s を使用してください。_creat and _wcreat have been deprecated; use _sopen_s, _wsopen_s instead.
構文Syntax
int _creat(
const char *filename,
int pmode
);
int _wcreat(
const wchar_t *filename,
int pmode
);
パラメーターParameters
filenamefilename
新しいファイルの名前。Name of new file.
pmodepmode
アクセス許可の設定。Permission setting.
戻り値Return Value
これらの関数は正常に実行された場合、作成されたファイルにファイル記述子を返します。These functions, if successful, return a file descriptor to the created file. それ以外の場合、次の表に示すように、関数は-1 を返し、 errno を設定します。Otherwise, the functions return -1 and set errno as shown in the following table.
errno の設定errno setting | 説明Description |
---|---|
EACCESEACCES | filename には、既存の読み取り専用ファイルを指定するか、ファイルの代わりにディレクトリを指定します。filename specifies an existing read-only file or specifies a directory instead of a file. |
EMFILEEMFILE | ファイル記述子をこれ以上使用できません。No more file descriptors are available. |
ENOENTENOENT | 指定されたファイルが見つかりませんでした。Specified file could not be found. |
Filename が NULL の場合、「パラメーターの検証」で説明されているように、これらの関数は無効なパラメーターハンドラーを呼び出します。If filename is NULL, these functions invoke the invalid parameter handler, as described in Parameter Validation. 実行の継続が許可された場合、これらの関数は errno を EINVAL に設定し、-1 を返します。If execution is allowed to continue, these functions set errno to EINVAL and return -1.
リターン コードの詳細については、「_doserrno、errno、_sys_errlist、および _sys_nerr」を参照してください。For more information about these and other return codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.
解説Remarks
_Creat 関数は、新しいファイルを作成するか、既存のファイルを開き、切り捨てます。The _creat function creates a new file or opens and truncates an existing one. _wcreat は _creat のワイド文字バージョンです。_wcreat する filename 引数は、ワイド文字列です。_wcreat is a wide-character version of _creat; the filename argument to _wcreat is a wide-character string. _wcreat と _creat は同じように動作します。_wcreat and _creat 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 |
---|---|---|---|
_tcreat_tcreat | _creat_creat | _creat_creat | _wcreat_wcreat |
Filename によって指定されたファイルが存在しない場合は、指定されたアクセス許可の設定で新しいファイルが作成され、書き込み用に開かれます。If the file specified by filename does not exist, a new file is created with the given permission setting and is opened for writing. ファイルが既に存在し、そのアクセス許可設定によって書き込みが許可されている場合、 _creat は、ファイルを長さ0に切り詰め、前の内容を破棄して、書き込み用に開きます。If the file already exists and its permission setting allows writing, _creat truncates the file to length 0, destroying the previous contents, and opens it for writing. アクセス許可の設定である pmode は、新しく作成されたファイルにのみ適用されます。The permission setting, pmode, applies to newly created files only. 新しいファイルは、最初に閉じた後に、指定されたアクセス許可設定を受け取ります。The new file receives the specified permission setting after it is closed for the first time. 整数式には、マニフェスト定数 _S_IWRITE と _S_IREAD の一方または両方が含ま れてい ます。The integer expression pmode contains one or both of the manifest constants _S_IWRITE and _S_IREAD, defined in SYS\Stat.h. 両方の定数が指定されている場合は、ビットごとの or 演算子 ( | ) と結合されます。When both constants are given, they are joined with the bitwise or operator ( | ). Pmode パラメーターは、次のいずれかの値に設定されます。The pmode parameter is set to one of the following values.
値Value | 定義Definition |
---|---|
_S_IWRITE_S_IWRITE | 書き込みが許可されます。Writing permitted. |
_S_IREAD_S_IREAD | 読み取りが許可されます。Reading permitted. |
_S_IREAD | _S_IWRITE_S_IREAD | _S_IWRITE | 読み取りと書き込みが許可されます。Reading and writing permitted. |
書き込みアクセス許可が与えられない場合、ファイルは読み取り専用になります。If write permission is not given, the file is read-only. ファイルはすべて常に読み取り可能です。書き込みのみのアクセス許可を与えることはできません。All files are always readable; it is impossible to give write-only permission. これらのモード _S_IWRITE と _S_IREAD | _S_IWRITE は同等です。The modes _S_IWRITE and _S_IREAD | _S_IWRITE are then equivalent. _Creat を使用して開いたファイルは、 _SH_DENYNO で常に互換モード ( _sopenを参照) で開かれます。Files opened using _creat are always opened in compatibility mode (see _sopen) with _SH_DENYNO.
_creat は、アクセス許可を設定する前に、現在のファイルアクセス許可マスクを pmode に適用します ( _umaskを参照してください)。_creat applies the current file-permission mask to pmode before setting the permissions (see _umask). _creat は、主に以前のライブラリとの互換性のために用意されています。_creat is provided primarily for compatibility with previous libraries. Oflag パラメーターで _O_CREAT と _O_TRUNC を使用した _open の呼び出しは、 _creat と同じであり、新しいコードに適しています。A call to _open with _O_CREAT and _O_TRUNC in the oflag parameter is equivalent to _creat and is preferable for new code.
必要条件Requirements
ルーチンによって返される値Routine | 必須ヘッダーRequired header | オプション ヘッダーOptional header |
---|---|---|
_creat_creat | <io.h> | <sys/types.h>, <sys/stat.h>, <errno.h><sys/types.h>, <sys/stat.h>, <errno.h> |
_wcreat_wcreat | <io.h> または <wchar.h><io.h> or <wchar.h> | <sys/types.h>, <sys/stat.h>, <errno.h><sys/types.h>, <sys/stat.h>, <errno.h> |
互換性について詳しくは、「 Compatibility」をご覧ください。For more compatibility information, see Compatibility.
例Example
// crt_creat.c
// compile with: /W3
// This program uses _creat to create
// the file (or truncate the existing file)
// named data and open it for writing.
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int fh;
fh = _creat( "data", _S_IREAD | _S_IWRITE ); // C4996
// Note: _creat is deprecated; use _sopen_s instead
if( fh == -1 )
perror( "Couldn't create data file" );
else
{
printf( "Created data file.\n" );
_close( fh );
}
}
Created data file.
関連項目See also
低レベル i/oLow-Level I/O
_chmod、_wchmod_chmod, _wchmod
_chsize_chsize
_close_close
_dup、_dup2_dup, _dup2
_open、_wopen_open, _wopen
_sopen、_wsopen_sopen, _wsopen
_umask_umask