Home > Language Reference > File Functions > Write

Write

The Write function writes data to the specified file.

Usage

Used to write data to an external text file which can be used in other applications.

Syntax

ret=Write(<filename>,<buffer>)
or
ret=Write(<filename>,<buffer>,<n>)
or
ret=Write(<filename>,<buffer>,<separator>)
or
ret=Write(<filename>,<buffer>,CRLF)

Parameters

<filename>, string, name of the file where the data should be written. It is not necessary to create the file first ; if the file does not exist, it is created when the first write is performed. If the file already exists, the write is done after the existing data. If you use Unicode encoding, you need to create the file using CreateUnicodeFile function before calling Write function and write Unicode strings.

<buffer>, string, variable only. The content of this variable is written at the end of <filename>. The Write is always performed sequentially. It is not possible to change the write pointer.

<n>, integer, number of bytes to write. Only the first n bytes of <buffer> are written in <filename>. If <buffer> contains less than <n> characters, it is padded with spaces.

<separator>, string. If it is used, <buffer> is written at the end of <filename> and then <separator> is written.

CRLF, keyword, carriage-return-line-feed keyword (ASCII code representation : chr$(13)+chr$(10)). This character CRLF (end of line) is written after <buffer>.

Return value

ret, optional numeric return value. If the function succeeds, the return value is 0. If the function fails, the return value is a nonzero value (see File Functions Error Codes). 

See also

Read
WriteIni
WriteReg
WriteExcel

Examples

Write(file$,var$)

Write("c:\wttest\sample.txt", var$)

Write(file$,var$,80)

Write(file$,var$,n)

Write(file$,var$,CRLF)

Write(file$,var$,"sample")


'This script writes some data into a txt file and then Read function is used to read its records.
FileName$="c:\wttest\data.txt"
'Write 5 records in filename$
write(filename$,"record 1",CRLF)
write(filename$,"record 2",CRLF)
write(filename$,"record 3",CRLF)
write(filename$,"record 4",CRLF)
write(filename$,"record 5",CRLF)

'Read the file just created
repeat
    Read(FileName$,result$,CRLF)
    display$=display$+"\n\"+result$
until eof(FileName$)=1
'Display the results 
msgbox(display$,0,"data.txt File")