Previous Topic: CreatePipe - Create a PipeNext Topic: EOF - Check for the End-of-File


CreateFile - Create a New File

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

Note: On Windows 9x, do not use this function to create an .ini file. Windows OS creates the specified .ini file of a WriteIniSection() or WriteIniEntry().

The CreateFile function creates a file for writing.

Function format:

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

Specifies the name of the file.

mode

Specifies the mode in which to create the file. The value is one of the following predefined constants:

O_TEXT

(value 0, default) 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: