How to check if a file is open in COBOL?

I am trying to find a way to check if a file is open in COBOL so that I can open it if it is closed or close it if it is open.

Thnx.

+3
source share
2 answers

Check FILE STATUSand act accordingly.

Try the following:

Add FILE-STATUSbelow FILE-CONTROL, for example:


    FILE-CONTROL.
        SELECT  MYFILE ASSIGN MYDD
                ORGANIZATION SEQUENTIAL
                ACCESS       SEQUENTIAL
                FILE STATUS  MYFILE-STATUS.

Declare a variable FILE STATUSin WORKING-STORAGE as a value PIC X(2), for example:


           01 MYFILE-STATUS   PIC X(2).
              88 MYFILE-ALREADY-OPEN   VALUE '41'.

Then in PROCEDURE DIVISIONenter OPENfor your file. Immediately after that, check the value FILE STATUS as in:


    OPEN MYFILE....
    IF MYFILE-ALRADY-OPEN
       CLOSE MYFILE...
    END-IF
    IF MYFILE-STATUS <> '00'
       perform some sort of general error routine
    END-IF

FILE STATUS, "9", COBOL, "41" COBOL. , - "9", . COBOL FILE STATUS: http://www.simotime.com/vsmfsk01.htm

+5

API, CBL_CHECK_FILE_EXIST, Micro Focus COBOL, AcuCOBOL Fujutsu COBOL.

, Micro Focus COBOL:

 copy "cblproto.cpy".

 program-id. MYMAIN.
 working-storage section.
 01  .
     05  file-details    cblt-fileexist-buf.

 procedure division.
     call 'CBL_CHECK_FILE_EXIST' using 'mymain.cbl '
                                       file-details
     if  return-code not = 0
       display "File mymain.cbl does not exist (or error)"
     else
       display "File mymain.cbl size is " cblt-fe-filesize
       of file-details
     end-if
 end program MYMAIN.
+1

Source: https://habr.com/ru/post/1731992/


All Articles