PreviousFile Organizations File StatusNext

Chapter 3: Filenames

This chapter covers:

3.1 Filename Conventions

This COBOL system, and applications created with it, use the standard UNIX filename conventions.

3.2 Assigning Filenames

The ASSIGN clause in the SELECT statement is used to specify either a physical filename or a logical name which can be mapped to a physical name at run time.

There are three types of filename assignment:

Run-time mapping of filenames is available for all three types of filename assignment.

3.2.1 Static Filename Assignment

With static filename assignment, the filename is specified in the SELECT statement as a literal:

select filename
   assign to literal.

In the following example, opening stockfile causes the warehs.buy file in the current directory to be opened:

select stockfile
   assign to "warehs.buy".

In this example, opening input-file opens the file prog in the data directory (relative to the current directory):

select input-file
   assign to "data/prog".

3.2.2 Dynamic Filename Assignment

With dynamic filename assignment, the filename is specified in the SELECT statement as a COBOL data item:

select filename
   assign to dynamic data-item

where the parameter is:

data-item The name of a COBOL data item. If the data item is not explicitly declared within your program, the Compiler creates one for you, with a picture of PIC X(255). Before the OPEN statement for the file is executed, the program must give a value to the data item.

If you use the ASSIGN"DYNAMIC" Compiler directive, you can omit the word DYNAMIC from the ASSIGN clause.

In the following example, the file input.dat is created in the current directory:

...
select fd-in-name
   assign to dynamic ws-in-file.
...
working-storage section.
01 ws-in-file     pic x(30).
...
move "input.dat" to ws-in-file.
...
open output fd-in-name.

3.2.3 External Filename Assignment

With external filename assignment, the filename is specified in the SELECT statement as follows:

select filename
   assign to external external-file-reference

where the parameter is:

external-file-reference A COBOL word that identifies the specified file to the external environment for possible further mapping. If external-file-reference contains one or more hyphens, all characters up to and including the last hyphen are ignored.

See the section Filename Mapping for further details on run-time filename mapping.

3.3 Filename Mapping

This COBOL system provides several ways of mapping the filename supplied by the program via the ASSIGN clause onto a different name, for greater flexibility at run time.

When the File Handler is presented with a filename (which may be a literal, the contents of a data item, or, in the case of the ASSIGN TO EXTERNAL syntax, an external reference), it isolates the first element of that name, that is, all the text before the first slash character (/), all the text if the name does not include such a character, or nothing if the filename starts with a slash character. Having done that, it:

The result is then considered to be the filename of the physical file.

Consider the following examples:

Filename in ASSIGN Clause Environment Variable Searched For Contents of Environment Variable Filename of Physical File
dir/file1 dd_dir d2 d2/file1
dir/file1 dd_dir d2/d4 d2/d4/file1
dir1/dir2/file1 dd_dir1 d4 d4/dir2/file1
dir1/dir2\file1 dd_dir1 d2/d4 d2/d4/dir2/file1
file1 dd_file1 d2 d2

Although the period ( . ) is not allowed in the names of environment variables, it is often used in filenames, so you may want to map logical filenames containing this character. In this case, you must replace the "." with the underscore character ( _ ). For example:

dd_file_PNT=/usr/qa/myfile.pnt

refers to the logical filename file.PNT.

3.3.1 Multiple Paths

An environment variable used for filename mapping can specify multiple pathnames. This causes the system to search for subsequent files if a "file not found" condition is returned for the first path specified by the environment variable.

Consider the following example contents of an environment variable named dd_dir:

dd_dir=/a/b;/c/d;

This causes the system to search /c/d for the assigned file if a "file not found" condition is returned on /a/b.


Notes:


3.4 Assignment of Operating System Device Names

You can write a COBOL program to send a report directly to the printer, or to transfer data across a communications port. To do this, you need to assign a device-name to your COBOL filename.

The following device-names can be specified using static, dynamic or external filename assignment:

CON Console keyboard or screen
PRN First parallel printer
LPT1 First parallel printer
LPT2 Second parallel printer
LPT3 Third parallel printer
COM1 First asynchronous communications port
COM2 Second asynchronous communications port

When specifying any of these device-names, a trailing colon(:) is optional.

In the following example, read or write operations on fd-name cause data to be read from or written to the console screen:

select fd-name
   assign to "con".

In this example, write operations on fd-name cause data to be output to lpt1:, the first parallel printer:

select fd-name
   assign to dynamic ws-filename.
   ...
   move "lpt1:" to ws-filename.

3.4.1 Setting Up Pipes

You can use COBOL file syntax to launch another process (such as the dir command) and either write data to the standard input of that other process, or read data coming from the standard output of the other process. The COBOL file organization must be either LINE SEQUENTIAL or RECORD SEQUENTIAL.

3.4.1.1 Output Pipes

To launch a process and write data to its standard input, the filename must consist of the > sign followed by the name of the command. The file should be opened for output.

For example:

select output-file
   assign to ">lpr"
   organization is line sequential.
...
open output output-file
write output-file-record from "Hello world".

In this example the program passes the characters "Hello world" to the standard input of the print process.

3.4.1.2 Input Pipes

To launch a process and read data from its standard output, the filename must consist of the "lt" symbol followed by the name of the command. The file should be opened for input.

For example:

select input-file
   assign to "<ls"
   organization is line sequential.
...
open input input-file
read input-file

In this example the program launches the ls process and reads in the first line which that process writes to its standard output.

3.4.1.3 Two-way Pipes

Two-way pipes combine the functions of input and output pipes. To use a two-way pipe the filename must consist of the pipe symbol (|) followed by the name of the command. The file should be opened for input-output (i-o).

For example:

select i-o-file
   assign to "| sort"
   organization is line sequential.
...
   open i-o i-o-file
   write i-o-file-record from "Hello world"
   read i-o-file

In this example the program launches the sort process and passes the line "Hello world" to its standard input. It then reads one record from the standard output of the sort process.


Copyright © 2000 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.

PreviousFile Organizations File StatusNext