前のトピック: CreatePipe - パイプの作成次のトピック: EOF - ファイルの終わりのチェック


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 が返されます。

例:

この例では、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: