Top > Lycia reference > Querix 4GL > Statements > OPEN ... > OPEN FILE
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.
OPEN FILE statement is used to open a file containing some sort of text, usually separated by delimiters in order to read the information from in into the program variables, or to record the program variables values into it. Here is its syntax:
Element |
Description |
Descriptor |
An integer variable that contains the file descriptor |
Character Exp |
A quoted character string or a variable of a character data type that contains the name of the file with the optional path |
Option List |
A list of options separated by commas that define the opening mode and other settings. |
If the OPEN FILE statement was executed successfully, it stores the file descriptor as a positive integer value into the file descriptor variable. There may be no direct dependence between the file descriptor value returned by OPEN FILE and the system-level file descriptor.
Descriptor is an integer variable that contains a positive integer. It is important that all your open file sessions have different values stored in their descriptors.
Though the descriptors of the two opened files are different variables, they may have the same value stored. Typically it happens if a non-initialized variables are used as a descriptor - in this case all descriptors will store 0. Lycia will produce an error in this case, since the file descriptors values should be unique among all open files.
OPTIONS clause defines whether the file is opened in the write, read or append mode, what delimiter and what file format is used.
There is the list of possible options. Note that all the options may be present at the same time, e.g. read and write options are not mutually exclusive.
Option |
Description |
READ ↓ |
Opens the file in read mode. |
WRITE ↓ |
Opens the file in write mode. |
APPEND ↓ |
Opens the file in append mode. |
CREATE |
Creates the file specified for opening, if the file does not already exist. |
EXCLUSIVE |
Only used together with the CREATE option. If the file you try to open already exists, the EXCLUSIVE option throws an error. |
DELIMITER ↓ ="delimiter" |
This option specifies the delimiter that is used in a file of the "load" format. |
FORMAT ↓ ="format" |
The format of the opened file. The format defines the default delimiters used, there are three possible options: "load", "csv", and "text" |
Files opened in the read mode can be read into the program variable. If there is no WRITE or APPEND option together with READ, the file contents cannot be changed. Its contents gets read according to the delimiters, default or custom. For more information about reading mode see the READ statement below in this guide.
Files opened in the write mode can be recorded into the file from the program variables. Its contents gets written according to the delimiters, default or custom. While the file session is active, the WRITE statement records new values at the bottom of the file. Every new open file session with the write mode opens the file with the offset position at the beginning of the file, so the file contents gets overwritten.
OPEN FILE f_desc FROM "/temp/clients.txt"
OPTIONS(READ, WRITE, FORMAT = "text")
Files opened in the append mode can be recorded into the file from the program variables. Its contents gets written according to the delimiters, default or custom. Every new open file session with the append mode opens the file with the offset position at the end of the file, so the file contents does not get overwritten.
OPEN FILE f_desc FROM "pricelist.csv"
OPTIONS(APPEND, FORMAT = "csv")
There are three formats a file can be opened in:
text - this format means a standard text file, that is read and written by full lines. The default delimiter for this type of file is new line symbol (\n). You cannot assign any other delimiters using DELIMITER option - an error will occur.
csv - this format means a csv file whose values are separated by the comma (,) delimiter. A different delimiter cannot be assigned for this file format.
"load" - this format means a standard unl file or any other type of file whose contents is separated by the delimiters. The default delimiter is pipe (|). The default delimiter can be changed by the means of the DELIMITER option.
DELIMITER option can be used to set a custom delimiter to the files of the "load" format. By default the pipe (|) is the default delimiter for the "load" file format. If you intend to use the default delimiter, it can be omitted. If you try to specify any other delimiter than the default delimiter for a text (default delimiter is \n) or csv (default delimiter is ,) file, a compile time error will occur.
Here is an example of a custom delimiter for an unl file:
OPEN FILE f_desc FROM "config.unl"
OPTIONS(READ, APPEND, CREATE, FORMAT = "load", DELIMITER="^")