Top  >  Lycia reference  >  Querix 4GL  >  Statements  >  OPEN ...  >  OPEN FILE

OPEN FILE

 

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

 

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

 

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"

 

READ

 

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.

 

WRITE

 

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")

 

APPEND

 

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")

 

FORMAT

 

There are three formats a file can be opened in:

 

DELIMITER

 

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="^")