Page 1 of 1

Writing a record in to PS File

PostPosted: Fri Nov 19, 2010 7:08 pm
by sriraj1122
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                   
    ELSE                                     
       DISPLAY 'OPEN FAIL'                   
       DISPLAY WS-STATUS-IN                   
    END-IF.                                   
    READ FILE1.                               
    IF WS-STATUS-IN = 0               
       DISPLAY 'READ SUCCESS'         
       DISPLAY INPUT-REC               
    ELSE                               
       DISPLAY 'READ FAIL'             
       DISPLAY WS-STATUS-IN           
    END-IF.                           
    MOVE '999999' TO INPUT-REC.       
    WRITE INPUT-REC.                   
    IF WS-STATUS-IN = 0               
       DISPLAY 'WRITE SUCCESS'         
    ELSE                               
       DISPLAY 'WRITE FAIL'           
       DISPLAY WS-STATUS-IN           
    END-IF.                           
    CLOSE FILE1.                       
    STOP RUN.                         


when I opened the file1 in I-O Mode and executing the program im getting the below result.
******
OPEN SUCCES
00
READ SUCCESS
999999
WRITE FAIL
48
*******

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.

Regards,
Sriraj K

Re: Writing a record in to PS File

PostPosted: Fri Nov 19, 2010 7:35 pm
by MrSpock
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.

Re: Writing a record in to PS File

PostPosted: Fri Nov 19, 2010 8:13 pm
by mickeywhite
When opening a file in i-o mode, you only can do a read or a rewrite. you can not do a write.

Re: Writing a record in to PS File

PostPosted: Sat Nov 20, 2010 1:17 am
by dick scherrer
Hello,

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.