|File Organizations||File Status|
This chapter covers:
This COBOL system, and applications created with it, use the standard DOS filename conventions.
It also supports the New Technology File System (NTFS) file naming conventions. This means that each file or directory can consist of up to 254 characters and contain both space characters and any number of periods (.). This COBOL system regards any text following the final period as an extension (or a trailing period as a space extension) and may remove it when creating its own names.
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:
The filename is specified as a literal in the ASSIGN clause of the SELECT statement.
The filename is specified as a data-item in the ASSIGN clause of the SELECT statement, and so can be changed by the program at run time.
The filename is specified as EXTERNAL in the ASSIGN clause of the SELECT statement and is resolved at run time.
Run-time mapping of filenames is available for all three types of 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 on the b: drive to be opened:
select stockfile assign to "b:warehs.buy".
In this example, a WRITE statement causes data to be output to prn:, the first parallel printer:
select printfile assign to "prn:".
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".
With dynamic filename assignment, the filename is specified in the SELECT statement as a COBOL data-item:
select filename assign to dynamic data-item
|data-item||is 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.
With external filename assignment, the filename is specified in the SELECT statement as follows:
select filename assign to external external-file-reference
|external-file-reference||is a COBOL word that identifies the specified file to the external environment for possible further mapping. If external-file-reference contains one or more hypens, all characters up to and including the last hypen are ignored.|
See the section Filename Mapping for further details on run-time 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.
In the following discussion the term "environment variable" should be read as including the Micro Focus extended environment variables which are enabled via use of the External File Mapper (see the section External File Mapper later in this chapter).
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 backslash character (\), all the text if the name does not include such a character, or nothing if the filename starts with a backslash 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||Envrionment Variable Searched For||Contents of Environment Variable||Filename of Physical File|
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:
This causes the system to search \c\d for the assigned file if a "file not found" condition is returned on \a\b.
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
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.
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.
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.
select output-file assign to ">cmd /c print" 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.
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.
select input-file assign to "<cmd /c dir" organization is line sequential. ... open input input-file read input-file
In this example the program launches the dir process and reads in the first line which that process writes to its standard output.
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 i-o.
select i-o-file assign to "| cmd /c 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.
You can assign filenames externally using the External File Mapper (Mfextmap). This provides a flexible method of mapping the assigned filename used within your COBOL program to a physical filename, by enabling filename mapping to be resolved within a text file (the mapper file). Filename mappings can be altered subsequently, simply by editing the file.
select filename assign to external assigned-name
|filename||The logical (internal) filename used by your program.|
|assigned-name||An entry in the mapper file.|
Using the External File Mapper, rather than resolving filename assignments via operating system environment variables, reduces the amount of memory required for environment space.
Before you can use the External File Mapper, you must create an ordinary text file (use a text editor such as Notepad to do this) and call it mfextmap.dat.
Each line within this file contains a filename assignment and takes the following form:
where the parameters are:
|assigned-name||The assigned filename used within your program.|
|physical-name||The actual name of the physical file, including the pathname.|
You can alter the contents of the mapper file during the execution of your program - the mapping information in the current version of the mapper file is always used.
If you create a mapper file, it must be called mfextmap.dat and it must be located in one of the following places:
Both MFEXTMAP and COBDIR can have multiple paths defined.
If MFEXTMAP is set but the mapper file does not exist in the directory or directories to which it points, the system attempts to resolve the filename by searching for an environment variable with the same name as the assigned filename used within the program.
If MFEXTMAP is not set, the system searches for the mapper file in the current directory and then along the COBDIR path. If the mapper file is not found, the system attempts to resolve the filename by searching for an environment variable with the same name as the assigned filename used within the program.
In order to use the External File Mapper:
Then, to activate the External File Mapper you must set the run-time tunable environment_mapper to TRUE. You do this by adding the following line:
to the $COBDIR\cobopt.cfg configuration file, or any configuration file pointed to by the COBCONFIG environment variable.
By disabling the External File Mapper you will increase the speed of your system as no disk accesses are required to search for the current version of the mapper file.
To disable the External File Mapper set the run-time tunable environment_mapper to FALSE.
Copyright © 1998 Micro Focus Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.
|File Organizations||File Status|