Work Files

#### Search Results

Search

# Work Files

Work files are files to which data can be written and from which data can be read by Natural programs. They are used for intermediate storage of data and for data exchange between programs. Data can be transferred from or to a work file by using the Natural statements `READ WORK FILE` and `WRITE WORK FILE`, or `UPLOAD PC FILE` and `DOWNLOAD PC FILE`.

This document covers the following topics:

- Defining Work Files

- Work File Formats

- Special Considerations for Work Files with the Extension NCD

- Using the Work File Type Transfer

## Defining Work Files

Using the Configuration Utility or the `DEFINE WORK FILE` statement, you can assign names (including the path) for up to 32 work files.

The maximum number of work files that can be used depends on the setting of the parameter `WORK`.

If you run a program which uses a work file for which a name and path has not been assigned, Natural automatically creates the file name and writes the work file into the temporary directory specified in the local configuration file. The name of such a file consists of the specified work file number and an arbitrary number assigned by the operating system. The generation of the work file name is based on an algorithm which tries to generate a unique name. Depending on the Natural parameter `TMPSORTUNIQ`, the naming convention may vary. If work file names are referenced from outside Natural, it is recommended that you specify the names explicitly to avoid problems identifying the files.

The following topics are covered below:

- Defining Work File Names with the Configuration Utility

- Defining Work File Names with Environment Variables

- Defining Work File Names with an Application Programming Interface

### Defining Work File Names with the Configuration Utility

In the Configuration Utility, the work file names are assigned in the category Work Files of a parameter file. The above mentioned parameters `WORK` and `TMPSORTUNIQ` can also be found in this category. See Work File Assignments in the Configuration Utility documentation for further information.

Tip
Locate the work file assignments by searching for "Work Files". See Finding a Parameter in the Configuration Utility documentation for further information.

### Defining Work File Names with Environment Variables

The following topics are covered below:

- About Defining Work File Names with Environment Variables
- Delimiters of Environment Variables

#### About Defining Work File Names with Environment Variables

Work files can also be defined by using Linux environment variables. Once you have defined the work file names in the parameter file, the work file names can be set without further change to the parameter file. For example, when you specify the following name for a work file in the parameter file (or in a `DEFINE WORK FILE` statement):

    $NATURAL/$myfile

and assume the following settings in your operating system:

    set NATURAL=/usr/natural
    set myfile=sub/test

this will expand into the following file name:

    usr/natural/sub/test

Note
Since the different shells interpret the tilde character (~) in different ways, this character is not interpreted by Natural.

#### Delimiters of Environment Variables

Names of environment variables are delimited by special characters. A left-hand delimiter is to the left of a variable, a right-hand delimiter is to the right.

For example, the string `$TEMP` identifies an environment variable named `TEMP`; `$` is used as both the left-hand and right-hand delimiter.

Valid delimiters are:

Type of Delimiter | Valid Delimiters
--- | ---
Left-hand delimiter | $
Right-hand delimiter | / .

### Defining Work File Names with an Application Programming Interface

You can also define work files with the application programming interface `USR1050N` in library `SYSEXT`.

## Work File Formats

The format of a work file depends on the work file type that has been defined. Different work file formats are available. Natural recognizes the format by checking the file name and its extension:

    file-name.extension

where file-name can have a maximum of 8 characters and extension can have a maximum of 3 characters.

The work file formats are:

- Binary Format

- ASCII Format

- Entire Connection Format

- Portable Format

- Unformatted Format

- CSV Format

See also Work Files and Print Files in the Unicode and Code Page Support documentation.

### Binary Format

Possible type: SAG.

This format is the preferred format since it can be used with all data types. However, it is not portable across platforms with different endian modes.

Each record that is written is preceded by two bytes which contain the length of the record. The length itself is written in a platform-specific form.

To define binary format for a work file, use a file name with a period and the extension SAG (for example, <file-name>.SAG).

### ASCII Format

Possible types: ASCII and ASCII compressed.

Since each written record is terminated with a line feed (LF), ASCII format is only recommended for alphanumeric data.

To define ASCII format for a work file, enter either a file name with a period and any extension except SAG and NCD (for example, <file-name>.<ext>), or a file name with a period and without an extension (for example, <file-name>).

### Entire Connection Format

Possible types: Entire Connection and Transfer.

Work files can be accessed in two different ways:

- Locally on Linux. The work file type Entire Connection is used for this purpose.

- Via a data transfer with Entire Connection. The work file type Transfer is used for this purpose. The data are sent to Entire Connection which writes the data to the PC.

The product Entire Connection uses two files: a data file which contains the actual data and a format file which contains formatting information about the data in the data file.

Natural automatically generates the corresponding format file for the type Entire Connection. The format file has the same name as the data file, however the extension is NCF. For detailed information on the content of a format file with the extension NCF, see the Entire Connection documentation.

When using the type Transfer, the format file is generated by the product Entire Connection (provided that the option Create format file has not been deactivated in the user properties; see the Entire Connection documentation for further information).

To define Entire Connection format for a work file, enter a file name with a period and the extension NCD (for example, <file-name>.NCD).

You can read/write work files in Entire Connection format directly from/to your local disk.

See also Special Considerations for Work Files with Extension NCD.

Notes:

1. The `RECORD` option of the `READ WORK FILE` statement is not available for reading work files of format Entire Connection.
2. The operand format U (Unicode) is not supported for the work file types Entire Connection and Transfer. If U is used with these work file types, a runtime error message is displayed.

### Portable Format

Possible type: Portable.

The type Portable performs an automatic endian conversion of a work file when the work file is transferred to a different machine. For example, a work file written on a PC (little endian) can be read correctly on an RS6000 or HP machine (big endian). The endian conversion applies only to field formats I2, I4, F4, F8 and U. The floating point format is assumed to be IEEE. There are, however, slight differences in IEEE floating point representation by different hardware systems. As a rule, these differences apply only to infinity and NaN representations, which are normally not written into work files. Check the hardware descriptions if you are uncertain.

The files are always written in the machine-specific representation, so that a conversion is performed only if the file is read by a machine with different representation. This keeps performance as fast as possible.

There are no other conversions for this format apart from the conversions mentioned above.

When a `READ WORK FILE` statement is used for a dynamic variable, the variable is resized to the length of the current record.

### Unformatted Format

Possible type: Unformatted.

The type Unformatted reads or writes a complete file with just one dynamic variable and just one record (for example, to store a video which was read from a database). No formatting information is inserted; everything is written and read just as it is.

### CSV Format

Possible type: CSV (comma-separated values).

Note
If you want to use the work file type CSV, you have to recatalog your sources using the `CATALOG` or `STOW` command. It is not possible to use the work file type CSV with generated programs of Natural Version 4.

The Natural fields are stored in a CSV work file as described below.

1. In the first step, the internal field data is converted into a readable format:

   - The field data of the internal Natural data formats B (binary), O (object handle), G (GUI handle) and C (attribute control) is copied to the record without field conversion. The data is taken as it is.

   - The field data of the internal Natural data format A (alphanumeric) is converted into the specified work file code page (see Work Files in the Configuration Utility documentation). If no work file code page is specified in the Configuration Utility, the default code page which is defined with the parameter `CP` is used and no conversion is done.

     The field data of the internal Natural data format U (Unicode), is converted into the specified work file code page (see Work Files in the Configuration Utility documentation) or, if no work file code page is specified, into the default code page which is defined with the parameter `CP`.

   - The values of the internal Natural formats D (date) and T (time) are converted into an alphanumeric output format. The `DTFORM` parameter is evaluated so that the user-specified date and time format is used.

   - The internal field values of the numeric types are converted into an alphanumeric output format.

2. In the second step, the field data in readable format is copied to the CSV work file record. The fields in the work file are separated by the specified separator character. If a field contains special characters, the field is delimited by double quotes. Each written record is terminated with a carriage return and line feed (CR/LF).

If you have defined that a header with the Natural field names is to be written to the work file (see Work File Assignments in the Configuration Utility documentation), the following applies:

- With the `WRITE WORK FILE` statement, a header line containing the field names of the first written record is stored in the first line of the work file. If subsequent CSV records contain a different number of fields, it may be possible that the header line does not correspond to these subsequent CSV records.

- With the `READ WORK FILE` statement, it is assumed that the first line of the CSV work file is the header line. Therefore, the first line is skipped (that is: the record data in the first line is not returned).

## Special Considerations for Work Files with the Extension NCD

If files with the extension NCD are created by Entire Connection and are then read into Natural via the `READ WORK FILE` statement, it is required that the Entire Connection option Keep trailing blanks is activated in the session properties. See your Entire Connection documentation for further information.

Note
When you create an NCD file using Entire Connection and load this file using the Object Handler, you may receive an error indicating that the source control record is missing. To avoid this, make sure that the option Keep trailing blanks is active when you create the NCD file.

The following considerations apply for work files in Entire Connection format:

- If an NCD file is read with a `READ WORK FILE` statement and the corresponding NCF format file is not available or contains invalid information, the NCD file is assumed to be an ASCII work file.

- When the `APPEND` attribute is used to append data to an NCD file, the record layouts (that is: the field format and length information which is written to the NCF format file) of the old and new data must match. If the record layouts are different, an error occurs during runtime.

- The maximum work-file record size for `WRITE WORK FILE VARIABLE` that can be handled by Entire Connection is 32767 bytes.

- If you have "old" work files with the extension NCD, the extensions must be changed.

- Each of the following profile parameters must be set to the same value for both read and write operations:

  `DC` (decimal character)
  `IA` (input assign character)
  `ID` (input delimiter character)

- Remember that the range of possible values for floating point variables on z/OS is different from that on other platforms. The possible value range for F4 and F8 variables on z/OS is:

      ±5.4 * 10-79 to ±7.2 * 1075

  The possible value range on most other platforms for F4 variables is:

      ±1.17 * 10-38 to ±3.40 * 1038

  The possible value range on most other platforms for F8 variables is:

      ±2.22 * 10-308 to ±1.79 * 10308

- A Natural error message is returned if DBMS calls are issued during an Entire Connection data transfer and their number exceeds the limit for DBMS calls permitted between screen I/Os (specified with the profile parameter `MADIO`). To circumvent this error, the application programming interface `USR1068N` in library `SYSEXT` is provided. `USR1068` resets the database call counter to zero (0). It must be invoked each time a DBMS call is issued during data transfer.

## Using the Work File Type "Transfer"

With local access (that is, without any data transfer being involved), you can read/write work files in Entire Connection format directly from/to your local disk. However, work files in Entire Connection format can also be accessed by using a data transfer. Both methods can be used simultaneously, but with different work file numbers only.

Work files to be accessed by using a data transfer (type Transfer) must be in Entire Connection format (NCD).

With data transfer, the Natural statements `READ WORK FILE` and `WRITE WORK FILE` do not read from and/or write to your local disk, but transfer the data to a PC that runs Entire Connection. The read/write operations are then done by Entire Connection from/to the disk of the PC.

For the work file number to be used, you have to set the profile parameter `ECPMOD` to `ON` in the Configuration Utility. It is not required that you assign a work file name in this case, because Entire Connection prompts you to enter a file name.

### On this page

- Defining Work Files
- Work File Formats
- Special Considerations for Work Files with the Extension NCD
- Using the Work File Type "Transfer"