Microsoft KB Archive/65747

Bad Results Declaring Multiple Records of Varying Length in FD

PSS ID Number: Q65747 Article last modified on 11-07-1990

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

Summary: Declaring multiple records of varying lengths for a single file description (FD) in the FILE SECTION of a COBOL program can cause incorrect data to be written to the file at run time. This is due to the fact that all records declared for a single FD are each allocated a buffer that has the same starting point in memory. When putting data into a record (using MOVE), any data previously put in the other records declared for the FD may be overwritten. Also, when a record is written (using WRITE), the entire buffer area is sent to disk, not just the number of characters that equal the length of the record. Note that this is not a problem, but is the standard behavior of COBOL. 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 (PDS) version 4.00 for MS-DOS and MS OS/2.

More Information: The sample code below demonstrates the behavior. Two records are declared for one file description. The first is a 1-byte field, the second, a 2-byte field. Data is entered into both fields, but it is the last data entered that actually gets written to disk. Also, even though the first WRITE statement uses the 1-byte field, 2 bytes get written. To compile and link the program in COBOL 3.00 or 3.00a, enter the following commands from the DOS command prompt: COBOL sample; LINK sample; The compiling and linking lines for COBOL PDS 4.00 are as follows: COBOL sample; LINK sample,,,/nod coblib cobapi;

SAMPLE.CBL
IDENTIFICATION DIVISION. PROGRAM-ID. SAMPLE. * Declare the sequential file: ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT OutFile ASSIGN TO &quot;FILE.DAT&quot;. * Define the records for the file: DATA DIVISION. FILE SECTION. FD OutFile. 01 Record1  PIC X.   01 Record2   PIC XX. * Open the file, write each record, and close the file: PROCEDURE DIVISION. OPEN OUTPUT OutFile. MOVE &quot;A&quot; TO Record1. * The &quot;B&quot; in &quot;BC&quot; will overwrite the &quot;A&quot; just written to the * buffer. MOVE &quot;BC&quot; TO Record2. WRITE Record1. WRITE Record2. CLOSE OutFile. You might expect that the contents of FILE.DAT would be the following: ABC However, because COBOL does not use different buffers for each record, and it always writes out the maximum number of bytes declared, FILE.DAT will contain the following: BCBC

Copyright Microsoft Corporation 1990.