Previous Topic: EOF - Check for the End-of-FileNext Topic: OpenPipe - Open a Pipe


OpenFile - Open a File

Valid on NetWare, Symbian OS, UNIX, Windows, and Windows CE

The OpenFile function opens a file for reading or writing.

Function format:

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

Specifies the name of the file to open.

access

Specifies how to open the file. The access parameter can be any of the following predefined constants:

O_READ

(value 0) Opened for reading.

O_WRITE

(value 1) Created for writing. This overwrites the file if it exists.

O_APPEND

(value 2) Opened for writing at end of file. This creates a file if it does not exist.

O_UPDATE

(value 3) Opened for reading and writing.

mode

Indicates an optional parameter to specify binary mode. The mode parameter can be one of the following predefined constants:

O_TEXT

Value 0; text mode

O_BINARY

Value 1; binary mode

If the mode parameter is omitted, text mode is assumed.

If the function succeeds, the return value is a non-negative integer, the file handle. If the function fails, it returns -1.

Example:

This example creates a backup of the CONFIG.SYS file.

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: