WRITE
Part of the Files and Reports section of Coddy's COBOL Programming journey. Lesson 6 of 22.
OPEN OUTPUT opens a file for writing. It creates the data set, or empties it when it already exists.
WRITE adds one record to the file. Fill the fields of the FD record, then write the record:
MOVE CUST-NAME TO L-NAME.
WRITE LIST-REC.A READ names the file and a WRITE names the record. WRITE LIST-FILE does not compile: GnuCOBOL says WRITE requires a record name.
The files written in these tasks go to the screen. ASSIGN TO DISPLAY prints each record as a line, the way a mainframe sends a report to the job's printed output:
SELECT LIST-FILE ASSIGN TO DISPLAY
ORGANIZATION IS LINE SEQUENTIAL.A line sequential record is written without its trailing spaces.
Challenge
EasyFor each customer, WRITE-ONE must move CUST-ID to L-ID and CUST-NAME to L-NAME, then write LIST-REC. LIST-FILE then prints lines such as C001 Ann Lee.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. WRITE1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUST-FILE ASSIGN TO CUSTIN
ORGANIZATION IS LINE SEQUENTIAL.
SELECT LIST-FILE ASSIGN TO DISPLAY
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD CUST-FILE.
01 CUST-REC.
05 CUST-ID PIC X(4).
05 CUST-NAME PIC X(10).
05 CUST-CITY PIC X(6).
05 CUST-BAL PIC 9(5).
FD LIST-FILE.
01 LIST-REC.
05 L-ID PIC X(5).
05 L-NAME PIC X(10).
WORKING-STORAGE SECTION.
01 WS-EOF PIC X VALUE "N".
88 END-OF-FILE VALUE "Y".
PROCEDURE DIVISION.
MAIN-PARA.
OPEN INPUT CUST-FILE OUTPUT LIST-FILE.
PERFORM UNTIL END-OF-FILE
READ CUST-FILE
AT END SET END-OF-FILE TO TRUE
NOT AT END PERFORM WRITE-ONE
END-READ
END-PERFORM.
CLOSE CUST-FILE LIST-FILE.
STOP RUN.
WRITE-ONE.
*> Fill LIST-REC and write it