Previous Topic: SeekFile - Reposition the Current Position in an Open FileNext Topic: File System Functions


WriteFile - Write Data to a File

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

The WriteFile function writes data to a file.

Function formats:

WriteFile(handle as Integer, buffer as String) as Boolean
WriteFile(handle as Integer, buffer as String, buflen as Integer) as Boolean
WriteFile(handle as Integer, buffer as Void) as Boolean
WriteFile(handle as Integer, buffer as Void, buflen as Integer) as Boolean
handle

Identifies a file handle returned by OpenFile or CreateFile.

buffer

Indicates a variable of any type.

buflen

Specifies the number of characters to be stored in the file. If the number of char exceeds buflen, only the first buflen characters are stored in the file; otherwise, the whole buffer is stored.

On successful completion, the function returns TRUE; otherwise, returns FALSE.

Note: Do not mix the different formats of the WriteFile() function formats. Mixing the formats may lead to unexpected results.

The interpreter handles the first format differently; and when writing the string to disk, the interpreter always adds a newline sign.

The interpreter maps the first format to a different system write function than the other three formats, which are mapped to a common system write function. The different systems write functions may use different caches to store the data written. At close time, the caches are written to disk one after another. If you mix the formats, the data may not appear in the correct sequence.

The interpreter handles the following formats in a different way:

WriteFile(handle as Integer, buffer as String) as Boolean 

When the string is written to disk, a newline is always added. This is not true for the other types of formats. The interpreter maps this function to a different system write function than the other three formats which were mapped to a common system write function. These different systems write functions may also use different caches to store the data written. At close time, the caches are written to disk one after another. If the formats are mixed, the data may not appear in the sequence - in which it was written. Therefore, do not mix this format with the other formats.

Example:

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

Rem 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

Rem ...Then create the output file...

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

CloseFile(fIn)
	Goto End
End if

Rem ...Copy lines until none left...

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

Rem ...Close Files, and signal success.

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

end:

More Information:

OpenFile - Open a File

CreateFile - Create a New File