Read a file with three records, only one of expected records output

1.5k Views Asked by At
IDENTIFICATION DIVISION.  
PROGRAM-ID. PROGRAM1.  
ENVIRONMENT DIVISION.  
INPUT-OUTPUT SECTION.  
FILE-CONTROL.  
SELECT EMP-GRADE ASSIGN TO 'input.txt'  
 ORGANIZATION IS SEQUENTIAL  
 ACCESS MODE IS SEQUENTIAL  
 FILE STATUS IS WS-STATUS.   
DATA DIVISION.  
FILE SECTION.  
FD EMP-GRADE.  
 01 NEWFILE.  
  05 FS-EMPID   PIC 9(5).  
  05 FS-NAME    PIC A(5).  
  05 FS-STREAM  PIC X(5).  
  05 FS-GRADE   PIC A(1).  
  05 FILLER     PIC X(64).  
WORKING-STORAGE SECTION.  
01 WS-EOF PIC A(1) VALUE "N".  
01 WS-STATUS PIC X(2).  
PROCEDURE DIVISION.  
MAIN-PARA.  
    OPEN INPUT EMP-GRADE.  
    PERFORM PARA1 THRU PARA1-EXIT UNTIL WS-EOF="Y".  
    CLOSE EMP-GRADE.  
    STOP RUN.  
MAIN-PARA-EXIT.  
    EXIT.  
PARA1.  
    READ EMP-GRADE  
        AT END MOVE "Y" TO WS-EOF  
        NOT AT END   
         IF FS-GRADE='A'  
          DISPLAY FS-EMPID , FS-NAME , FS-STREAM , FS-GRADE  
         END-IF  
END-READ.  
PARA1-EXIT.  
    EXIT.  

input provided:

1234 sita comp A  
2345 tina main B  
5689 riya math A  

but the output is coming :

1234 sita comp A 

It is reading only the first record.

1

There are 1 best solutions below

0
On

As Brian Tiffin is hinting at in the comments, it is your data which is the problem.

This:

  05 FILLER     PIC X(64).

Means that your records should be 64 bytes longer than they are.

If you have a fixed-length record, or only fixed-length records, under an FD, then the data all have to be the same length, and equal to what you have defined in your program.

It means, and behaviour depends on compiler, you only have one record as far as the COBOL program is concerned.

A good way to spot such things is to always count your input records, and count your output records, and records which should not be selected for output. You can then easily tell if anything has fallen between a crack.

Leaving that aside, here's your program with some adjustments:

 IDENTIFICATION DIVISION.  
 PROGRAM-ID. PROGRAM1.  
 ENVIRONMENT DIVISION.  
 INPUT-OUTPUT SECTION.  
 FILE-CONTROL.  
 SELECT EMP-GRADE ASSIGN TO 'input.txt'  
  ORGANIZATION IS SEQUENTIAL  
  ACCESS MODE IS SEQUENTIAL  
  FILE STATUS IS WS-STUDENT-GRADE-STATUS.   
 DATA DIVISION.  
 FILE SECTION.  
 FD EMP-GRADE.  
 01  NEWFILE.  
     05  FS-EMPID                        PIC 9(5).  
     05  FS-NAME                         PIC X(5).  
     05  FS-STREAM                       PIC X(5).  
     05  FS-GRADE                        PIC X(1).  
     05  FILLER                          PIC X(64).  
 WORKING-STORAGE SECTION.  
 01  WS-STUDENT-GRADE-STATUS             PIC X(2).
     88  END-OF-STUDENT-GRADE-FILE       VALUE "10".  
     88  ERROR-ON-STUDENT-GRADE-FILE     VALUE ZERO.
 PROCEDURE DIVISION.  
     OPEN INPUT                   EMP-GRADE
*    perform a paragraph to check FILE STATUS field is zero, using an 88.

     PERFORM                      PRIMING-READ
     PERFORM                      PROCESS-STUDENT-GRADE-FILE
       UNTIL                      END-OF-STUDENT-GRADE-FILE
     CLOSE                        EMP-GRADE  
*    perform a paragraph to check FILE STATUS field is zero, using an 88.
     GOBACK  
     .
 PRIMING-READ.
     PERFORM                      READ-STUDENT-GRADE
     .
 READ-STUDENT-GRADE.
     READ EMP-GRADE  
*    perform a paragraph to check FILE STATUS field is zero, using an 88.
     .

 PROCESS-STUDENT-GRADE-FILE.  
     IF FS-GRADE='A'  
*    To see the problem with your data, DISPLAY the 01-level
         DISPLAY                  NEWFILE
         DISPLAY                  FS-EMPID 
                                  FS-NAME  
                                  FS-STREAM FS-GRADE  
     END-IF  
     PERFORM                      READ-STUDENT-GRADE
     .

If you use the FILE STATUS field, you should check it. Since you use it, you can use it to check for end-of-file without the AT END. If you use a "priming read" you don't need the AT END/NOT AT END tangle. If you code the minimum of full-stops/periods in the PROCEDURE DIVISION, you won't have a problem with them. Commas are never needed, so don't use them. Format your program for human readability. Use good descriptive names for everything. The THRU on a PERFORM invites the use of GO TO. As a learning, avoid the invitation.

If your class itself enforces particular ways to code COBOL, you'll have to use those ways. If so, I'd suggest you do both. The first couple of times at least, submit both to your tutor. Even if they tell you not to do that, continue doing dual examples when given tasks (just don't submit them any more). There is no reason for you to start off with bad habits.

Keep everything simple. If your code looks bad, make it look good through simplification and formatting.

Remember also that COBOL is all about fixed-length stuff. Get's us back to your original problem.