Microsoft KB Archive/58959: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (X010 moved page Microsoft KB Archive/Q58959 to Microsoft KB Archive/58959 without leaving a redirect: Text replacement - "Microsoft KB Archive/Q" to "Microsoft KB Archive/")
 
(No difference)

Latest revision as of 19:25, 12 August 2020

LINE SEQUENTIAL File Truncates Trailing Spaces in COBOL 3.00

PSS ID Number: Q58959 Article last modified on 09-06-1990

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

Summary: When records of a LINE SEQUENTIAL file are written out to disk with the WRITE statement, all trailing spaces are truncated and the carriage return and linefeed characters (CR/LF) are appended immediately after the last nonspace character in the record. This is always the case, no matter what syntax is used or what directives are used in compiling the program. A LINE SEQUENTIAL file that keeps the trailing spaces can be simulated by using a RECORD SEQUENTIAL (referred to as just SEQUENTIAL) file organization and manually writing out the CR/LF at the end of each record. This information applies to Microsoft COBOL Compiler 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 variable-length LINE SEQUENTIAL records (instead of fixed-length records as in the example further below). To find this article, query in this Knowledge Base on the following words: truncate AND spaces AND COBOL LINE SEQUENTIAL files do not truncate the trailing spaces of each record in earlier versions of Microsoft COBOL (versions 2.00, 2.10, 2.20). This is a feature of Micro Focus COBOL; therefore, it has been included into versions 3.00 and 3.00a of Microsoft COBOL. This feature 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. Note that the LINE SEQUENTIAL file organization is not part of the COBOL ANSI standard, but is an EXTRA organization added by Microsoft and Micro Focus. The following sample program illustrates the behavior of LINE SEQUENTIAL files. It writes out a LINE SEQUENTIAL file containing two records. Even though both records have the same length (80 characters) as stated by the file description, the actual records written to the disk file have different lengths because the number of trailing spaces for the two records are different. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT file-1 ASSIGN TO “file.dat” ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD file-1. 01 record-1 PIC X(80). PROCEDURE DIVISION. OPEN OUTPUT file-1. * 80 - 3 characters leaves 77 trailing spaces. MOVE “abc” TO string-1. * 5 characters will be written. WRITE record-1. * 80 - 7 characters leaves 73 trailing spaces. MOVE “abc def” TO string-1. * 9 characters will be written. WRITE record-1. CLOSE file-1. STOP RUN. When run, the above program creates a file called FILE.DAT, which contains two records. The first record is five characters long, consisting of “abc” and a CR/LF. The second record is nine characters long, consisting of “abc def” and a CR/LF. The records are of different lengths because they have a different number of trailing spaces (77 for the first, and 73 for the second) and these spaces are truncated before the CR/LF characters are added and the record is written to disk. As mentioned above, a LINE SEQUENTIAL file that keeps the trailing spaces on records can be simulated by using a SEQUENTIAL file of fixed-length records and manually appending the CR/LF to the end of each record. The following sample program shows how to write out a file in LINE SEQUENTIAL format (that is, with a CR/LF at the end of each record) and not have the trailing spaces of each record truncated. Each record in the file will have the exact same length, regardless of how many trailing spaces it has. This is done using the SEQUENTIAL file format and manually writing the CR/LF characters at the end of each record. To show that the created file indeed conforms to LINE SEQUENTIAL format, the program reopens it for INPUT with LINE SEQUENTIAL organization and reads the records back in. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. * The SEQUENTIAL output file. SELECT file-1 ASSIGN TO “file.dat” ORGANIZATION IS SEQUENTIAL. * The LINE SEQUENTIAL input file. SELECT file-2 ASSIGN TO “file.dat” ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD file-1. 01 record-1. * The output data. 05 string-1 PIC X(80). * The CR/LF appended to each record. 05 cr-lf PIC XX VALUE X“0D0A”. FD file-2. * The input data. 01 record-2 PIC X(80). PROCEDURE DIVISION. OPEN OUTPUT file-1. * 80 - 3 characters leaves 77 trailing spaces. MOVE “abc” TO string-1. * 82 characters will be written. WRITE record-1. * 80 - 7 characters leaves 73 trailing spaces. MOVE “abc def” TO string-1. * 82 characters will be written. WRITE record-1. CLOSE file-1. OPEN INPUT file-2. * Read the records in LINE SEQUENTIAL format. READ file-2. DISPLAY record-2. READ file-2. DISPLAY record-2. CLOSE file-2. STOP RUN. When run, the above program writes out two records that have exactly the same length, 80 characters followed by a CR/LF. The first record is “abc” plus 77 spaces and the CR/LF. The second record is “abc def” plus 73 spaces and the CR/LF. The spaces are actually written out to disk because the organization is SEQUENTIAL, not LINE SEQUENTIAL. The CR/LF, normally automatically written after each record when the organization is LINE SEQUENTIAL, is manually written after each record by putting X“0D0A” (hex 13 plus hex 10) into the field following the field containing the actual data. These two fields are then written out as a group item. The screen output for the program is as follows: abc abc def

Copyright Microsoft Corporation 1990.