Microsoft KB Archive/65474

Variable-Length LINE SEQUENTIAL Records with Trailing Spaces

PSS ID Number: Q65474 Article last modified on 09-21-1990

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

Summary: Below is a sample COBOL program that creates a LINE SEQUENTIAL file with variable-length records that have trailing spaces. It does this by opening the file as RECORD SEQUENTIAL and writing the records out one byte at a time. This allows a variable amount of data and trailing spaces to be written, plus the manual insertion of a carriage return/linefeed (CR/LF) sequence at the end of each record. The CR/LF sequence is required if the file is to qualify as having LINE SEQUENTIAL organization. This information applies to Microsoft COBOL versions 3.00 and 3.00a for MS-DOS and MS OS/2.

More Information: A separate article describes how to write trailing spaces to fixed-length (instead of variable-length) LINE SEQUENTIAL records and gives more information on the workings of LINE SEQUENTIAL files. To find this separate article, query in this Knowledge Base on the following words: truncate and spaces and COBOL Under Microsoft COBOL versions 3.00 and 3.00a, when a record is written to a LINE SEQUENTIAL file, all trailing spaces are truncated and a CR/LF sequence is appended to the end of the record. LINE SEQUENTIAL files whose records do not have their trailing spaces truncated can be simulated by opening the file as RECORD SEQUENTIAL (SEQUENTIAL) and manually appending a CR/LF sequence to each record. With RECORD SEQUENTIAL files, trailing spaces of records are not truncated. This technique works with fixed-length records but not with variable-length ones. To achieve variable-length records that can have trailing spaces but are also terminated with a CR/LF sequence, you can manually write out the record one byte at a time: first the data, then any trailing spaces desired, and finally the CR/LF sequence. Note that this will work only with alphanumeric data (PIC X), since reference modification is used. Although Microsoft COBOL versions 2.00, 2.10, and 2.20 do not truncate trailing spaces off the records of a LINE SEQUENTIAL file, such truncation is a feature of Micro Focus COBOL and was thus implemented in versions 3.00 and 3.00a of Microsoft COBOL. This is documented on Page 5-29 of the “Microsoft COBOL Compiler 3.0: Language Reference Manual” as follows When LINE SEQUENTIAL ORGANIZATION is specified either implicitly or explicitly, the file is treated as consisting of variable length records, each record containing one line of data. and on Page 7-13 of the “Microsoft COBOL Compiler 3.0: Operating Guide” as follows: On output, any trailing spaces in the program’s record area are removed. The code sample (SAMPLE.CBL) demonstrates the use of a 1-byte record to write variable-length records with trailing spaces that are terminated with CR/LF sequences. To compile and link the program, enter the following commands from the DOS command prompt: cobol sample; link sample;

SAMPLE.CBL
$SET ANS85 IDENTIFICATION DIVISION. PROGRAM-ID. Sample. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. *   The SEQUENTIAL output file: SELECT OutFile ASSIGN TO &quot;FILE.DAT&quot; ORGANIZATION IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD OutFile. * The one-byte output record: 01 OneByteField    PIC X.   WORKING-STORAGE SECTION. * Use to circulate through the bytes of Field1 and Field2: 01 ByteCounter  PIC 99. * Two fields of varying length with trailing spaces: 01 Field1       PIC X(25) VALUE &quot;abcdef                   &quot;. 01 Field2       PIC X(10) VALUE &quot;abc       &quot;. PROCEDURE DIVISION. *   Open the file for output. OPEN OUTPUT OutFile. *   Output first field one byte at a time: PERFORM VARYING ByteCounter FROM 1 BY 1 UNTIL ByteCounter > 25 WRITE OneByteField FROM Field1(ByteCounter:1) END-PERFORM. *   Append carriage return/linefeed: MOVE X&quot;0D&quot; TO OneByteField. WRITE OneByteField. MOVE X&quot;0A&quot; TO OneByteField. WRITE OneByteField. *   Output second record one byte at a time: PERFORM VARYING ByteCounter FROM 1 BY 1 UNTIL ByteCounter > 10 WRITE OneByteField FROM Field2(ByteCounter:1) END-PERFORM. *   Append carriage return/linefeed: MOVE X&quot;0D&quot; TO OneByteField. WRITE OneByteField. MOVE X&quot;0A&quot; TO OneByteField. WRITE OneByteField. *   Close the output file and end: CLOSE OutFile. STOP RUN. Copyright Microsoft Corporation 1990.