Writing a record in to PS File

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

Writing a record in to PS File

Postby sriraj1122 » Fri Nov 19, 2010 7:08 pm

I need a small clarification in reading & writing PS Files in cobol. I have written the below piece of code .

FILE SECTION.                                 
FD FILE1                                     
       LABEL RECORDS ARE STANDARD             
       BLOCK CONTAINS 0 RECORDS               
       RECORDING MODE F                       
       DATA RECORD IS INPUT-REC.             
01 INPUT-REC.                                 
   05 POLICY-NUMBER   PIC X(20).             
WORKING-STORAGE SECTION.                     
01 WS-STATUS-IN PIC 9(02) VALUE ZERO.         
PROCEDURE DIVISION.                           
    OPEN I-O    FILE1.           
    IF WS-STATUS-IN = 0                       
       DISPLAY 'OPEN SUCCES'                 
       DISPLAY WS-STATUS-IN                   
       DISPLAY 'OPEN FAIL'                   
       DISPLAY WS-STATUS-IN                   
    READ FILE1.                               
    IF WS-STATUS-IN = 0               
       DISPLAY 'READ SUCCESS'         
       DISPLAY INPUT-REC               
       DISPLAY 'READ FAIL'             
       DISPLAY WS-STATUS-IN           
    MOVE '999999' TO INPUT-REC.       
    WRITE INPUT-REC.                   
    IF WS-STATUS-IN = 0               
       DISPLAY 'WRITE SUCCESS'         
       DISPLAY 'WRITE FAIL'           
       DISPLAY WS-STATUS-IN           
    CLOSE FILE1.                       
    STOP RUN.                         

when I opened the file1 in I-O Mode and executing the program im getting the below result.

But when I opened FILE1 in EXTEND MODE / OUTPUT MODE. Im able to write the records in to FILE1 but not able to read the record.

How can I achieve both READ & WRITE in a single program.

Help would be appreciated.

Sriraj K
Posts: 19
Joined: Thu Nov 18, 2010 10:04 am
Has thanked: 0 time
Been thanked: 0 time

Re: Writing a record in to PS File


Re: Writing a record in to PS File

Postby MrSpock » Fri Nov 19, 2010 7:35 pm

I'm not a COBOL expert, but at a higher level I don't understand what logic you're trying to use here. A PS dataset is typically either an input file, or an output file. Logically it can't serve as both at the same time. You can, however, open the file for inpuit, read it, and do what you want. Then close it, open it for output, or extend, do what you want, then close it. The only other option I see that would be relevant here is to open the file for I-O, READ, and when a specific record has been identified, use a REWRITE to update it.
User avatar
Global moderator
Posts: 811
Joined: Wed Jun 06, 2007 9:37 pm
Location: Raleigh NC USA
Has thanked: 0 time
Been thanked: 2 times

Re: Writing a record in to PS File

Postby mickeywhite » Fri Nov 19, 2010 8:13 pm

When opening a file in i-o mode, you only can do a read or a rewrite. you can not do a write.
Posts: 11
Joined: Tue Nov 02, 2010 8:04 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Writing a record in to PS File

Postby dick scherrer » Sat Nov 20, 2010 1:17 am


Most organizations will not permit code to overwrite input sequential datasets. If there is a problem in the middle of the run, the data is corrupted. . .

The code should read one file and write another.
Hope this helps,
User avatar
dick scherrer
Global moderator
Posts: 6308
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 90 times

Return to IBM Cobol


  • Related topics
    Last post