Top > Lycia reference > Querix 4GL > Statements > WRITE
To meet your needs, we constantly work to improve Querix products.
This means that Lycia documentation is developing as well.
In case you have found a certain dissonance between the provided information and the actual behavior of Lycia 3 and/or your applications, please, let us know about this via documentation@querix.com so that we can introduce the necessary changes to our documentation.
Thank you for your attention and cooperation.
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:
If the second OPEN FILE contains the WRITE option and no APPEND option, the file offset will be set at the beginning of the file and the written values will overwrite the data in the file. It does not rewrite the whole file, it rewrites only that part of the file starting from its beginning that corresponds to the size of the newly written values. If the file is longer than the values written, the remains of the file will be unchanged.
If the second OPEN FILE contains the APPEND option, the file offset will be set at the end of the file and each new WRITE statement will add new values at the end of the file rather than rewrite existing values.
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
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