2
votes

Hello I am total beginner in cobol and needing some homework help. I am trying to write a program that prints address labels on the ouput. But in the output there has to be a header, page number, and date. I have successfully got the program to print the addresses in label format but cannot seem to get the heading line (with the page and date) to show up above it. With my program the way it is there is an error code stating that I have the wrong access mode for the data file. I am unsure what this means. Here is my program. I got rid of the date part just to try and get the heading line in above the addresses. *EDIT: I have added the open and close for "print header out" but now it gives me the error code "file locked" Can anyone shed some light on this.

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.

   SELECT LABEL-FILE-IN
   ASSIGN TO 'C0603.DAT'
   ORGANIZATION IS LINE SEQUENTIAL.

   SELECT LABEL-FILE-OUT
   ASSIGN TO 'C0603.RPT'
   ORGANIZATION IS LINE SEQUENTIAL.

   SELECT PRINT-HEADER-OUT
   ASSIGN TO 'C0603.RPT'
   ORGANIZATION IS LINE SEQUENTIAL.

   DATA DIVISION.
   FILE SECTION.


   FD LABEL-FILE-IN.
   01 LABEL-RECORD-IN.
   05 CUST-NAME-IN PIC X(20).
   05 ADDRESS-IN PIC X(20).
   05 CITY-STATE-ZIP-IN PIC X(20).

   FD LABEL-FILE-OUT.
   01 LABEL-RECORD-OUT.
   05 PRINT-LABEL-OUT PIC X(20).

   FD  PRINT-HEADER-OUT.
   01  REPORT-OUT                  PIC X(80).


             WORKING-STORAGE SECTION.
   01  ARE-THERE-MORE-RECORDS PIC X(3) VALUE 'YES'.

   01  HEADING-LINE1.
       05                          PIC X(40) VALUE SPACES.
       05                          PIC X(12) VALUE
           "MAILING LIST".

   01  DATE-WS.
       05 MONTH-WS                 PIC XX.
       05 YEAR-WS                  PIC XX.

   01  DATE-WS-OUT.
       05                           PIC X(45) VALUE SPACES.
       05  MONTH-WS-OUT              PIC XX.
       05                          VALUE "/".
       05  YEAR-WS-OUT               PIC XX.


             PROCEDURE DIVISION.

   000-MAIN-MODULE.
       PERFORM 100-INITIALIZATION-MODULE.
       PERFORM 200-PROCESS-ONE-RECORD
           UNTIL ARE-THERE-MORE-RECORDS = "NO ".
       PERFORM 900-TERMINATION-MODULE.
       STOP RUN.

   100-INITIALIZATION-MODULE.
       OPEN OUTPUT PRINT-HEADER-OUT
       OPEN INPUT LABEL-FILE-IN
       OPEN OUTPUT LABEL-FILE-OUT
       ACCEPT DATE-WS FROM DATE.
       MOVE MONTH-WS TO MONTH-WS-OUT.
       MOVE YEAR-WS TO YEAR-WS-OUT.
       PERFORM 600-READ-MODULE.
       PERFORM 300-TOP-OF-PAGE-MODULE.

   200-PROCESS-ONE-RECORD.
       MOVE SPACES TO PRINT-LABEL-OUT

       MOVE CUST-NAME-IN TO PRINT-LABEL-OUT
       WRITE LABEL-RECORD-OUT

       MOVE ADDRESS-IN TO PRINT-LABEL-OUT
       WRITE LABEL-RECORD-OUT

       MOVE CITY-STATE-ZIP-IN TO PRINT-LABEL-OUT
       WRITE LABEL-RECORD-OUT


   PERFORM 600-READ-MODULE.

   300-TOP-OF-PAGE-MODULE.
       MOVE HEADING-LINE1 TO REPORT-OUT.
       WRITE REPORT-OUT AFTER ADVANCING 9 LINES.
       MOVE DATE-WS-OUT TO REPORT-OUT.
       WRITE REPORT-OUT AFTER ADVANCING 1 LINES.


   600-READ-MODULE.
       READ LABEL-FILE-IN
            AT END MOVE "NO " TO ARE-THERE-MORE-RECORDS
       END-READ.

   900-TERMINATION-MODULE.
       CLOSE PRINT-HEADER-OUT.
       CLOSE LABEL-FILE-IN.
       CLOSE LABEL-FILE-OUT.
2

2 Answers

3
votes

I think the problem you are having is that both LABEL-FILE and HEADER-FILE point to the same physically file ('C0603.RPT'). You can do this, but only one of them may be open at a time. This is the source of the "file locked" message when you try to open it a second time under a different name.

The typical way of doing this is to open one file but have multiple record definitions for writing to it.

Drop the:

    SELECT PRINT-HEADER-OUT
    ASSIGN TO 'C0603.RPT'
    ORGANIZATION IS LINE SEQUENTIAL.

and change the FD's for LABEL-FILE-OUT to include the Header record...

    FD LABEL-FILE-OUT.
    01.
       05 LABEL-BUFFER              PIC X(80).
       05 LABEL-RECORD-OUT REDEFINES LABEL-BUFFER.
          10 PRINT-LABEL-OUT PIC X(20).
          10                 PIC X(60).
       05 PRINT-HEADER-OUT REDEFINES LABEL-BUFFER.
          10 REPORT-OUT      PIC X(80).

There are other ways of doing this, but the basic idea is to have an output buffer that is the at least as big as the largest ouput record andREDEFINE it for multiple usages (LABEL or HEADER).

When writing a label line or header line just use WRITE LABEL-BUFFER and then move SPACES to it after each write to ensure it gets properly initialized before re-populating any of the subordiante data items.

0
votes

The "error code stating that I have the wrong access mode for the data file" is because the PRINT-HEADER-OUT file is not OPEN when you execute the statement WRITE REPORT-OUT. All files must be OPENed before they are used and should always be CLOSEd when you are finished with them.