Jack011
Jack011

Reputation: 59

Cobol open input file error message

What is the error handling command for open input line-sequential file?

I tried,

OPEN INPUT CUSTOMER-FILE
INVALID KEY/ON ERROR
DISPLAY "NO FILE FOUND".

but could not get it to work.

Thanks.

Upvotes: 1

Views: 4301

Answers (2)

bmakos
bmakos

Reputation: 179

I agree with using a file status, it is much more flexible and you can handle all errors in one solution. Here is most of the code you need for it:

FILE-CONTROL.                                                
    SELECT  FILENAME  ASSIGN  TO  FILENAME                 
                        FILE STATUS IS WS-FS-FILENAME.       
*I  used WS-FS-FILENAME so I know it's declared in Working Storage and connected to File *Section.

DATA DIVISION.                                               
FILE SECTION.                                                

FD  FILENAME            RECORDING  ....                         
                        BLOCK      ...                        
                        RECORD     ....                       
                        LABEL      RECORD    ....        
                        DATA       RECORD    ...
                        .....  

01  FILENAME-REC         PIC X(N).




WORKING-STORAGE SECTION.                                       
01  WS-FS-FILENAME   PIC XX.   

==================================================


OPEN OUTPUT FILENAME.                        
IF WS-FS-FILENAME NOT = '00' THEN            
   DISPLAY 'ERROR OPENING FILENAME'          
   DISPLAY 'ERROR CODE IS : ', WS-FS-FILENAME
   PERFORM EXIT-WITH-ERROR                        
END-IF.                                     

WRITE FILENAME-REC.                                   
IF WS-FS-FILENAME NOT = '00'                          
   DISPLAY 'WRITE ERROR ON FILENAME. ' 
   DISPLAY 'STATUS :' WS-FS-FILENAME                  
   PERFORM EXIT-WITH-ERROR                                
END-IF. 

And so on with reading it, closing it.

You can find the specific error codes in the Cobol documentation as well.

Upvotes: 0

cschneid
cschneid

Reputation: 10765

In FILE-CONTROL, add a FILE STATUS clause to the SELECT for your file and in the PROCEDURE DIVISION check the value of the file status variable you specified against the documented values after each file interaction.

Upvotes: 3

Related Questions