cobolgnucobol

Keep trailing spaces when writing to file organization line sequential


I'm using GnuCOBOL and writing to a file, organization is line sequential, and I need to keep trailing spaces. Line sequential appends the [CR][LF] characters, but does so after removing trailing spaces.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. TRAILING-SPACES.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT OUTFILE 
           ASSIGN 'O:\BATCH_JOBS\TMP\TRAILING-SPACES.COBOL.TXT'
           ORGANIZATION IS LINE SEQUENTIAL.

       DATA DIVISION.
       FILE SECTION.
       FD OUTFILE
           DATA RECORD IS OUT-RECORD.

       01 OUT-RECORD.
          05 STUFF PIC X(5).

       PROCEDURE DIVISION.
           OPEN OUTPUT OUTFILE.

           MOVE 'ABCDE' TO STUFF.
           WRITE OUT-RECORD.

           MOVE 'ABC  ' TO STUFF.
           WRITE OUT-RECORD.

           CLOSE OUTFILE.
           STOP RUN.

The output is

ABCDE[CR][LF]

ABC[CR][LF]

How would I get the output to be

ABCDE[CR][LF]

ABC[SPACE][SPACE][CR][LF]

I tried declaring the file as organization is sequential and generate the [CR][LF] but I can't seem to get it to work.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. TRAILING-SPACES.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT OUTFILE 
           ASSIGN 'O:\BATCH_JOBS\TMP\TRAILING-SPACES.COBOL.TXT'
           ORGANIZATION IS SEQUENTIAL.

       DATA DIVISION.
       FILE SECTION.
       FD OUTFILE
           DATA RECORD IS OUT-RECORD.

       01 OUT-RECORD.
          05 STUFF PIC X(5).
          05 CR PIC X(1) VALUE X'0D'.
          05 LF PIC X(1) VALUE X'0A'.

       PROCEDURE DIVISION.
           OPEN OUTPUT OUTFILE.

           MOVE 'ABCDE' TO STUFF.
           WRITE OUT-RECORD.

           MOVE 'ABC  ' TO STUFF.
           WRITE OUT-RECORD.

           CLOSE OUTFILE.
           STOP RUN.

The output is

ABCDE[NUL][NUL]ABC[SPACE][SPACE][NUL][NUL]

Thanks everyone!


Solution

  •        01 OUT-RECORD.
              05 STUFF PIC X(5).
              05 CR PIC X(1) VALUE X'0D'.
              05 LF PIC X(1) VALUE X'0A'.
    

    These VALUE clauses are ignored in the FILE SECTION.

    There are several ways to adjust for that, but the easiest is to place the code in the WORKING-STORAGE SECTION.

           01 OUT-RECORD PIC X(7).
    
           WORKING-STORAGE SECTION.
           01 WS-OUT-RECORD.
              05 STUFF PIC X(5).
              05 CR PIC X(1) VALUE X'0D'.
              05 LF PIC X(1) VALUE X'0A'.
    

    Then

               MOVE "ABC" TO STUFF
               WRITE OUT-RECORD FROM WS-OUT-RECORD