Microsoft KB Archive/59404

COBOL 3.00 Can’t Declare a SORT Work File as EXTERNAL

PSS ID Number: Q59404 Article last modified on 11-19-1990

3.00 3.00a 4.00 | 3.00 3.00a 4.00 MS-DOS | OS/2

Summary: A work file used by the SORT statement cannot be declared EXTERNAL. If it is, the SORT statement will not write the proper data to the output file. If the output file is LINE SEQUENTIAL, a pair of carriage return (CR) and linefeed (LF) characters is written for each record in the input file, but no data is written. If the output file is RECORD SEQUENTIAL (referred to as just SEQUENTIAL), a space is written for each character (excluding delimiters) in the input file; and again no actual data is written. If the program is compiled in protected mode for OS/2, it causes a protection violation. Note that the keyword EXTERNAL is not allowed in an SD (sort description) entry. However, the compiler fails to flag this as a syntax error. Also note that compiling with the /CALLFH directive makes all files EXTERNAL by default. This directive should not be used if the program uses the SORT statement. Instead, each file that needs to be declared EXTERNAL should have the EXTERNAL clause in its FD file description entry. This information applies to Microsoft COBOL Compiler versions 3.00 and 3.00a for MS-DOS and MS OS/2, and to Microsoft COBOL Professional Development System version 4.00 for MS-DOS and MS OS/2.

More Information: Three files are associated with the SORT statement: the input file, the output file, and the work file. The input file contains the unsorted records, the output file contains the sorted records, and the work file is used as a workspace to do the sorting. It is created by the SORT statement and deleted when the sorting is finished. It is this file, the work file, that cannot be declared as EXTERNAL in the SD entry. The program below illustrates the problem. To keep it simple, the file to be sorted (INPUT.DAT) contains only three records of one character each. Each record is terminated with a carriage return and linefeed character. Since carriage return, linefeed, and space are not visible characters, the string “CL” is used in the following example when referring to carriage return/linefeed. When referring to a space character, “S” is used. After execution, the file storing the sorted records (OUTPUT.DAT) does not contain the correct data. Under OS/2, the program causes a protection violation and does not complete the SORT. The contents of INPUT.DAT are as follows: bCL cCL aCL Compile and link the following program for DOS, as follows: COBOL sort.cob ; LINK sort extfh externl; Compile and link the program below for OS/2, as follows: PCOBOL sort.cob ; LINK sort extfh externl /nop,,, pcobol.lib doscalls.lib; The following code example demonstrates the problem: ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT work-file ASSIGN TO “WORK.DAT”. SELECT input-file ASSIGN TO “INPUT.DAT” ORGANIZATION IS LINE SEQUENTIAL. SELECT output-file ASSIGN TO “OUTPUT.DAT” ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD input-file. 01 input-record PIC X. FD output-file. 01 output-record PIC X. SD work-file IS EXTERNAL. 01 work-record PIC X. PROCEDURE DIVISION. SORT work-file ON ASCENDING KEY work-record USING input-file GIVING output-file. STOP RUN. After execution, OUTPUT.DAT contains the following: CL CL CL That is, a carriage return and a linefeed character are written for each record in the input file. If the ORGANIZATION for the output file is SEQUENTIAL, not LINE SEQUENTIAL, OUTPUT.DAT contains the following: SSS That is, a space is written for every record (excluding delimiters) in the input file. Finally, if the EXTERNAL clause is not used in the SD entry, the SORT statement writes the proper data to OUTPUT.DAT. All three records will be in order with a carriage return/linefeed delimiting them if the ORGANIZATION of the output file is LINE SEQUENTIAL: aCL bCL cCL If the ORGANIZATION of the output file is SEQUENTIAL, OUTPUT.DAT contains the following: abc

Copyright Microsoft Corporation 1990.