Reading Records of PS file in cobol



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Reading Records of PS file in cobol

Postby gowthamgyaa » Tue Oct 23, 2012 7:32 pm

Hi everyone,
I have 2 Records in my Flat file , but whenever i code to read those two, i end up in reading only first record, I request you to guide me how can i read all records,
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
            SELECT INFILE ASSIGN TO SYSUT1
            ORGANIZATION IS SEQUENTIAL
             ACCESS IS SEQUENTIAL
              FILE STATUS IS PSFS.
DATA DIVISION.
FILE SECTION.
FD INFILE
01 INREC
       05 E-NO PIC 9(2).
       05 E-NAME PIC A(10).
       05 E-ACCT PIC X(5).
       05 E-MOB PIC 9(10).
       05 FILLER X(53).
PROCEDURE DIVISION.
MAIN-PARA.
                 PERFORM OPEN-PARA THRU CLOSE-PARA.
                 STOP RUN.
OPEN-PARA.
                 OPEN INPUT INFILE.
                 IF PSFS NOT = '00'
                  DISPLAY "ERROR IN OPENING FILE:" PSFS
                  PERFORM CLOSE-PARA
                  END-IF.   
READ-PARA.
                 READ INFILE
                  IF PSFS NOT = '00'
                  DISPLAY "ERROR IN READING FILE:" PSFS
                  ELSE
                  PERFORM CLOSE-PARA
                  DISPLAY "CONTENT OF FLAT FILE:" INREC
                  END-IF.
CLOSE-PARA.
                  CLOSE INFILE.


I thought with my knowledge that inorder to read another Record in Flat file i should use another SELECT statement in environment division. But if i do that only the first record of the file is getting repeated once.


Kind regards
gyaa.
gowthamgyaa
 
Posts: 101
Joined: Wed Sep 05, 2012 11:18 pm
Has thanked: 67 times
Been thanked: 0 time

Re: Reading Records of PS file in cobol

Postby Robert Sample » Tue Oct 23, 2012 7:34 pm

In general, for each record you want to read from ANY input file in COBOL, you must execute a READ statement against that file. Since you only execute one READ statement, you only READ one record.

A SELECT statement is only used to specify entire files -- each file may have 1, 2, 10 million records (the record count may be relevant to your program but not to the SELECT statement).

These users thanked the author Robert Sample for the post:
gowthamgyaa (Tue Oct 23, 2012 7:35 pm)
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Reading Records of PS file in cobol

Postby gowthamgyaa » Tue Oct 23, 2012 7:44 pm

Hi Robert,
Thanks for your kind reply, If had used another read verb also but I was re-reading the first record once again.
Can i use perform in read?

READ-PARA.
            READ INFILE.
            IF PSFS NOT = '00'
            DISPLAY "ERROR IN READING:" PSFS
            PERFORM CLOSE-PARA
            ELSE
            PERFORM READ-PARA UNTIL INFILE > 2
            DISPLAY "CONTENTS OF FLAT FILES:" INREC
            END-IF.


i need one more clarification too , in the Read para if i use the code as READ INFILE INTO INREC, its showing me an error.
Also in display statement if i display as INFILE instead of INREC its showing me an error.


Regards
gyaa.
gowthamgyaa
 
Posts: 101
Joined: Wed Sep 05, 2012 11:18 pm
Has thanked: 67 times
Been thanked: 0 time

Re: Reading Records of PS file in cobol

Postby Pandora-Box » Tue Oct 23, 2012 7:45 pm

Just to add on to what Robert said

You need to do something like this below is the pseudocode


Open file
Read file
Perform until EOF
        Do      Process
        Read file
End Perform
Close file

These users thanked the author Pandora-Box for the post:
gowthamgyaa (Tue Oct 23, 2012 8:13 pm)
User avatar
Pandora-Box
 
Posts: 65
Joined: Fri Feb 10, 2012 8:30 pm
Location: Mars
Has thanked: 3 times
Been thanked: 6 times

Re: Reading Records of PS file in cobol

Postby Robert Sample » Tue Oct 23, 2012 8:01 pm

Study the pseudocode premkrishnan posted and learn how to translate that into COBOL. Almost every batch COBOL program you write will follow that same structure.

Other comments:
1. COBOL does not support recursion. Putting PERFORM READ-PARA inside READ-PARA is guaranted to fail.
2. READ <file name> INTO <record area> requires <record area> to be defined in WORKING-STORAGE. INREC in your code is the 01 under the FD and hence is NOT in WORKING-STORAGE.
3. In COBOL, you READ the file name because you do not know when you issue the READ how long the record is (and for variable length files, this is a consideration). You OPEN and CLOSE file names. Everywhere else in your program you will use the record name (the 01 under the FD) -- for DISPLAY, for WRITE, for MOVE, etc.
4. COBOL will not re-read a record from a sequential file. Each READ will read the next record in sequence. However, if you CLOSE the file and then OPEN it again, you will start reading from the first record again.

These users thanked the author Robert Sample for the post:
gowthamgyaa (Tue Oct 23, 2012 8:13 pm)
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post