前のトピック: CreateDirectory または MkDir - 新規ディレクトリの作成次のトピック: DeleteFile または DelFile - ファイルの削除


CreateFile - 新規ファイルの作成

NetWare、Symbian OS、UNIX、Windows、および Windows CE で有効です。

注: Windows 9x では、この関数を使用して .ini ファイルを作成しないでください。 通常、Windows OS は、 WriteIniSection() または WriteIniEntry() で指定 .ini ファイルを作成します。

CreateFile 関数は、書き込み用ファイルを作成します。

ファイルの内容に関するこの関数の形式は、以下のとおりです。

CreateFile(filename as String, mode as Integer) as Integer
CreateFile(filename as String) as Integer
filename

作成するファイル名を指定します。 指定した名前のファイルがある場合は、置き換えられます。

mode

ファイルを作成するディレクトリを指定します。 この値は、以下の定義済み定数のいずれかです。

O_TEXT

(値 0、デフォルト)テキスト モード

O_BINARY

(値 1)バイナリ モード

mode パラメータを省略すると、テキスト モードになります。

この関数に成功すると、戻り値は負でない整数(つまりファイル ハンドル)になります。 この関数が失敗すると、-1 が返されます。

例: CreateFile 関数

この例では、CONFIG.SYS ファイルのバックアップが作成されます。

Dim fIn, fOut as integer   ' Declare file handles
Dim OneLine as string     ' String to hold one line

' First open the Input file...

fIn=OpenFile("C:¥CONFIG.SYS",O_READ)
if fIn<0 then
	MessageBox("Unable to open input file","Error")
	Goto End
End if

' ...Then create the output file...

fOut=CreateFile("C:¥CONFIG.BAK")
if fOut<0 then
	MessageBox("Unable to create output file","Error")
	Goto End

End if

' ...Copy lines until none left...

while Not(Eof(fIn))
	if ReadFile(fIn,OneLine) then WriteFile(fOut,OneLine)
wend

' ...Close Files, and signal success.

CloseFile(fIn)
CloseFile(fOut)
MessageBox("A backup of the CONFIG.SYS file was created","MyScript")

end: