ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Reading a file in COBOL

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reading a file in COBOL

    i have written a COBOL which received a range of values. Say input that i receive is 10,55. That means the range is from 10 to 55.
    Based on this range i have to read a file. So i will have to read the records starting from 10 till 55.

    how to use the READ statement so that i will fetch the 10th record for processing? A normal READ will start reading the file from 1st record, then how to stop at 10th record? Or is there any way that i can directly go to 10th record?

  • #2
    Re: Reading a file in COBOL

    Code:
           ...
           FILE-CONTROL.
               SELECT MyFile ASSIGN TO DATABASE-MyFile
                 ORGANIZATION IS relative
                 ACCESS IS random
                 RELATIVE KEY IS RRN-MyFile.
           DATA DIVISION.
           FILE SECTION.
           FD  MyFile
           ...
           WORKING-STORAGE SECTION.
           01  RRN-MyFile      pic 9(5) comp-3.
    
           Procedure Division.
           Start-Proc.
    
               OPEN INPUT MyFile.
    
         * Read the 10th record of MyFile
               MOVE 10 TO RRN-MyFile.
               READ MyFile AT END display "NOT FOUND"
                   NOT AT END  ...           <-- Record found.
              ...
    Last edited by Mercury; September 17, 2010, 07:47 AM.
    Philippe

    Comment


    • #3
      Re: Reading a file in COBOL

      Thanks to sharing another one example is:

      It also shows how to check the open and read to make sure it was successfull.

      In your I/O section:
      INPUT-OUTPUT SECTION.
      FILE-CONTROL.
      SELECT FILE-IN ASSIGN TO FILE-NAME-IN
      FILE STATUS IS WS-STATUS-IN.

      In your data division:
      DATA DIVISION.
      FILE SECTION.
      FD FILE-IN
      RECORDING MODE IS V.
      01 SAMPLE-INPUT-PROF PIC X(4000).

      In working storage, define:
      05 WS-STATUS-IN PIC 9(02) VALUE ZERO.

      In your cobol code:
      OPEN INPUT SAMPLE-FILE
      IF WS-STATUS-IN = 0
      (You will loop through the reads)
      READ FILE-IN INTO WS-I-REC
      AT END
      (Do something like close the file when done)
      END-READ
      EVALUATE WS-STATUS-IN
      (Do something with various conditions)
      END-EVALUATE
      ELSE
      (Do something if you had an open error)
      END-IF





      -----------
      STC Technologies|Reputation Management

      Comment

      Working...
      X