Top  >  Lycia reference  >  Querix 4GL  >  Statements  >  WRITE

WRITE

 

WRITE statement is used to record value from the program variables into a file.

 

 

Element

Description

Descriptor

The integer variable that stores the file descriptor.

Variable List

The list of program variables separated by commas whose values are to be recorded into the file.

 

The descriptor should be first linked to a file by means of the OPEN FILE statement. While the file descriptor was opened and not closed, each new WRITE statement appends the values to the end of the file, even if the file was opened without the APPEND option and only with the WRITE option. If the CLOSE FILE was called for the file descriptor followed by another OPEN FILE option, the write statement can behave in two different ways:

 

Like with the READ statement the variable list can contain simple variables, records and arrays. E.g.:

 

WRITE TO file_dscr USING contacts.*

WRITE TO file_dscr USING var1, var2, var3

 

 

Variable list

 

Values of each variable in the variable list will be recorded to the file as an element separated from the rest by the delimiters. The WRITE statement inserts delimiters between values and records them to the file in the order in which they appear in the USING clause. E.g. when writing to a text file, each variable in the USING clause will be a new line.

 

If the values in the variables contain delimiters, e.g. LET a = "string1 \n string2", then they will be recorded with this delimiter and the variable 'a' in this case will be recorded as two lines into a text file.

 

The following example creates a text file and opens it for writing, then records 2 strings into it and closes the file. Next it opens the same file, rewrites the first line with the new value and closes the file again. The third opening is in the append mode, so the WRITE statement writes a third line into the file - the file will contain 3 lines at the end of the operation.

 

MAIN

DEFINE my_f INT

DEFINE s1, s2, s3 STRING

 

LET my_f = 101

LET s1 = "string1"

LET s2 = "string2"

 

OPEN FILE my_f FROM "text5.txt" OPTIONS (WRITE, CREATE, FORMAT="text")

      WRITE TO my_f USING s1

      WRITE TO my_f USING s2

CLOSE FILE my_f

LET s2 = "string3"

OPEN FILE my_f FROM "text5.txt" OPTIONS(WRITE, FORMAT="text")

      WRITE TO my_f USING s2

CLOSE FILE my_f

LET s3 = "string4"

OPEN FILE my_f FROM "text5.txt" OPTIONS(APPEND, FORMAT="text")

      WRITE TO my_f USING s3

CLOSE FILE my_f

END MAIN

 

#at this point the file contents will be:

#string3

#string2

#string4