如何检查文件是否已在 COBOL 中打开?

发布于 2024-08-20 13:55:14 字数 75 浏览 8 评论 0原文

我正在尝试找到一种方法来检查文件是否已在 COBOL 中打开,以便我可以在文件关闭时打开它,或者在打开时关闭它。

谢谢。

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

Thnx.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北恋 2024-08-27 13:55:14

检查文件状态并采取相应措施。

请尝试以下操作:

FILE-CONTROL 下添加 FILE-STATUS,例如:

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

WORKING-STORAGE 中声明一个 FILE STATUS 变量
作为 PIC X(2) 值,例如:

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

然后在PROCEDURE DIVISION中为您的程序发出OPEN
文件。紧接着,测试 FILE STATUS 的值
如:

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

第一个字符不是“9”的 FILE STATUS 的值为
COBOL 标准值,因此测试“41”以检测已打开的文件
应该适用于所有 COBOL 实现。当第一个字符是“9”时要小心,
这些是供应商特定的文件状态代码。查看以下链接
关于使用 COBOL FILE STATUS 的一个很好的介绍:http://www.simotime.com /vsmfsk01.htm

Check the FILE STATUS and act accordingly.

Try the following:

Add a FILE-STATUS under the FILE-CONTROL, for example:

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

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

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

Then in the PROCEDURE DIVISIONissue an OPEN for your
file. Immediately following that, test the value of 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

Values of FILE STATUS where the first character is not a '9', are
COBOL standard values so testing for '41' to detect an already open file
should work on all COBOL implementations. Beware when the first character is a '9',
these are vendor specific file status codes. Check out the following link for
a good introduction to using COBOL FILE STATUS: http://www.simotime.com/vsmfsk01.htm

煮酒 2024-08-27 13:55:14

您的编译器还可能提供外部 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.

Your compiler may also provide a external API, such as CBL_CHECK_FILE_EXIST which can be found on Micro Focus COBOL, AcuCOBOL and Fujutsu COBOL.

For example, on 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文