前のトピック: EOF - ファイルの終わりのチェック次のトピック: OpenPipe - パイプのオープン


OpenFile - ファイルのオープン

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

OpenFile 関数は読み書きのためにファイルを開きます。

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

OpenFile(filename as String, access as Integer, mode as Integer) as Integer
OpenFile(filename as String, access as Integer) as Integer
filename

オープンするファイル名を指定します。

アクセス

ファイルのオープン方法を指定します。 access パラメータには、以下のいずれかの事前定義された定数を指定できます。

O_READ

(値 0)読み取りのために開きます。

O_WRITE

(値 1)書き込みのために作成します。 指定したファイルが存在する場合は、上書きされます。

O_APPEND

(値 2)ファイルの終わりに、書き込みのために開きます。 指定したファイルが存在しない場合は、ファイルが作成されます。

O_UPDATE

(値 3)読み書きのために開きます。

mode

バイナリ モードを指定するオプション パラメータを指定します。 mode パラメータには、以下のいずれかの事前定義された定数を指定できます。

O_TEXT

値 0(テキスト モード)

O_BINARY

値 1(バイナリ モード)

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

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

例: OpenFile 関数

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