📜  COBOL-文件处理动词

📅  最后修改于: 2020-11-22 16:55:56             🧑  作者: Mango


文件处理动词用于对文件执行各种操作。以下是文件处理动词-

  • 打开
  • 改写
  • 删除
  • 开始

开放动词

打开是必须执行的第一个文件操作。如果打开成功,则只能对文件进行进一步的操作。仅在打开文件后,文件结构中的变量才可供处理。每次文件操作后,文件状态变量都会更新。

句法

OPEN "mode" file-name.

在这里,文件名是字符串字面量,您将使用它来命名文件。可以在以下模式下打开文件-

Sr.No. Mode & Description
1

Input

Input mode is used for existing files. In this mode, we can only read the file, no other operations are allowed on the file.

2

Output

Output mode is used to insert records in files. If a sequential file is used and the file is holding some records, then the existing records will be deleted first and then new records will be inserted in the file. It will not happen so in case of an indexed file or a relative file.

3

Extend

Extend mode is used to append records in a sequential file. In this mode, records are inserted at the end. If file access mode is Random or Dynamic, then extend mode cannot be used.

4

I-O

Input-Output mode is used to read and rewrite the records of a file.

阅读动词

读取动词用于读取文件记录。读取的函数是从文件中获取记录。在每个读取动词上,只能将一条记录读取到文件结构中。要执行读取操作,请在INPUT或IO模式下打开文件。在每个读取语句中,文件指针都会增加,因此将读取连续的记录。

句法

以下是在文件访问模式为顺序模式时读取记录的语法-

READ file-name NEXT RECORD INTO ws-file-structure
   AT END DISPLAY 'End of File'
   NOT AT END DISPLAY 'Record Details:' ws-file-structure
END-READ.

以下是使用的参数-

  • NEXT RECORD是可选的,并在顺序读取索引顺序文件时指定。

  • INTO子句是可选的。在WorkingStorage部分中定义了ws-file-structure,以从READ语句获取值。

  • 到达文件末尾时,AT END条件变为True。

示例-以下示例使用行顺序组织读取现有文件。该程序可以使用Live Demo选项进行编译和执行,它将显示文件中存在的所有记录。

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
      FILE-CONTROL.
      SELECT STUDENT ASSIGN TO 'input.txt'
      ORGANIZATION IS LINE SEQUENTIAL.            

DATA DIVISION.
   FILE SECTION.
   FD STUDENT.
   01 STUDENT-FILE.
      05 STUDENT-ID PIC 9(5).
      05 NAME PIC A(25).

   WORKING-STORAGE SECTION.
   01 WS-STUDENT.
      05 WS-STUDENT-ID PIC 9(5).
      05 WS-NAME PIC A(25).
   01 WS-EOF PIC A(1). 

PROCEDURE DIVISION.
   OPEN INPUT STUDENT.
      PERFORM UNTIL WS-EOF='Y'
         READ STUDENT INTO WS-STUDENT
            AT END MOVE 'Y' TO WS-EOF
            NOT AT END DISPLAY WS-STUDENT
         END-READ
      END-PERFORM.
   CLOSE STUDENT.
STOP RUN.

假设input.txt文件中可用的输入文件数据包含以下内容-

20003 Mohtashim M.
20004 Nishant Malik
20005 Amitabh Bachhan

当您编译并执行上述程序时,它将产生以下结果-

20003 Mohtashim M.            
20004 Nishant Malik           
20005 Amitabh Bachhan 

句法

以下是当文件访问模式为随机时写记录的语法-

READ file-name RECORD INTO ws-file-structure
   KEY IS rec-key
   INVALID KEY DISPLAY 'Invalid Key'
   NOT INVALID KEY DISPLAY 'Record Details: ' ws-file-structure
END-READ.

示例-以下示例使用索引组织读取现有文件。该程序可以在大型机上使用JCL进行编译和执行,它将显示文件中存在的所有记录。在大型机服务器上,我们不使用文本文件;相反,我们使用PS文件。

假设存在于大型机上的文件与上述示例中的input.txt文件具有相同的内容。

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
   SELECT STUDENT ASSIGN TO IN1
      ORGANIZATION IS INDEXED
      ACCESS IS RANDOM
      RECORD KEY IS STUDENT-ID
      FILE STATUS IS FS.

DATA DIVISION.
   FILE SECTION.
   FD STUDENT.
      01 STUDENT-FILE.
      05 STUDENT-ID PIC 9(5).
      05 NAME PIC A(25).
     
   WORKING-STORAGE SECTION.
   01 WS-STUDENT.
      05 WS-STUDENT-ID PIC 9(5).
      05 WS-NAME PIC A(25).

PROCEDURE DIVISION.
   OPEN INPUT STUDENT.
      MOVE 20005 TO STUDENT-ID.
      
      READ STUDENT RECORD INTO WS-STUDENT-FILE
         KEY IS STUDENT-ID
         INVALID KEY DISPLAY 'Invalid Key'
         NOT INVALID KEY DISPLAY WS-STUDENT-FILE
      END-READ.
      
   CLOSE STUDENT.
STOP RUN.

JCL执行上述COBOL程序-

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO
//IN1 DD DSN = STUDENT-FILE-NAME,DISP=SHR

当您编译并执行上述程序时,它将产生以下结果-

20005 Amitabh Bachhan 

写动词

写动词用于在文件中插入记录。记录写入后,记录缓冲区中将不再可用。在将记录插入文件中之前,将值移到记录缓冲区中,然后执行写动词。

Write语句可与FROM选项一起使用,以直接从工作存储变量中写入记录。 From是一个可选子句。如果访问模式是顺序访问,则要写入记录,文件必须在输出模式或扩展模式下打开。如果访问模式是随机的或动态的,则要写入记录,文件必须在输出模式或IO模式下打开。

句法

以下是文件组织为顺序组织时读取记录的语法-

WRITE record-buffer [FROM ws-file-structure]
END-WRITE.

以下是在对文件组织建立索引或相对索引时读取记录的语法-

WRITE record-buffer [FROM ws-file-structure]
   INVALID KEY DISPLAY 'Invalid Key'
   NOT INVALID KEY DISPLAY 'Record Inserted'
END-WRITE.

示例-以下示例显示了组织顺序时如何在新文件中插入新记录。

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
   SELECT STUDENT ASSIGN TO OUT1
      ORGANIZATION IS SEQUENTIAL
      ACCESS IS SEQUENTIAL
      FILE STATUS IS FS.

DATA DIVISION.
   FILE SECTION.
   FD STUDENT
   01 STUDENT-FILE.
      05 STUDENT-ID PIC 9(5).
      05 NAME PIC A(25).
      05 CLASS PIC X(3).

   WORKING-STORAGE SECTION.
   01 WS-STUDENT.
      05 WS-STUDENT-ID PIC 9(5).
      05 WS-NAME PIC A(25).
      05 WS-CLASS PIC X(3).

PROCEDURE DIVISION.
   OPEN EXTEND STUDENT.
      MOVE 1000 TO STUDENT-ID.
      MOVE 'Tim' TO NAME.
      MOVE '10' TO CLASS.
      WRITE STUDENT-FILE
      END-WRITE.    
   CLOSE STUDENT.
STOP RUN.

JCL执行上述COBOL程序-

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO
//OUT1 DD DSN = OUTPUT-FILE-NAME,DISP = (NEW,CATALOG,DELETE)

当您编译并执行上述程序时,它将在输出文件中添加一条新记录。

1000 Tim         10

重写动词

重写动词用于更新记录。应该以IO模式打开文件以进行重写操作。仅在成功执行读取操作后才能使用它。重写动词将覆盖最近读取的记录。

句法

以下是文件组织为顺序组织时读取记录的语法-

REWRITE record-buffer [FROM ws-file-structure]
END-REWRITE.

以下是在对文件组织建立索引或相对索引时读取记录的语法-

REWRITE record-buffer [FROM ws-file-structure]
   INVALID KEY DISPLAY 'Invalid Key'
   NOT INVALID KEY DISPLAY 'Record Updated'
END-REWRITE.

示例-以下示例显示如何更新在上一个Write步骤中插入的现有记录-

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
   SELECT STUDENT ASSIGN TO IN1
      ORGANIZATION IS INDEXED
      ACCESS IS RANDOM
      RECORD KEY IS STUDENT-ID
      FILE STATUS IS FS.

DATA DIVISION.
   FILE SECTION.
   FD STUDENT
   01 STUDENT-FILE.
      05 STUDENT-ID PIC 9(4).
      05 NAME PIC A(12).
      05 CLASS PIC X(3).
      
   WORKING-STORAGE SECTION.
   01 WS-STUDENT.
      05 WS-STUDENT-ID PIC 9(5).
      05 WS-NAME PIC A(25).
      05 WS-CLASS PIC X(3).

PROCEDURE DIVISION.
   OPEN I-O STUDENT.
   MOVE '1000' TO STUDENT-ID.
  
   READ STUDENT
      KEY IS STUDENT-ID
      INVALID KEY DISPLAY ‘KEY IS NOT EXISTING’
   END-READ.
  
   MOVE 'Tim Dumais' TO NAME.
   REWRITE STUDENT-FILE
   END-REWRITE.
   CLOSE STUDENT.
STOP RUN.

JCL执行上述COBOL程序-

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO
//IN1 DD DSN = OUTPUT-FILE-NAME,DISP = SHR

当您编译并执行上述程序时,它将更新记录-

1000 Tim Dumais  10

删除动词

删除动词只能在索引文件和相关文件上执行。该文件必须在IO模式下打开。在顺序文件组织中,不能删除记录。在顺序访问模式下,将删除Read语句最后读取的记录。在随机访问模式下,指定记录密钥,然后执行删除操作。

句法

以下是删除记录的语法-

DELETE file-name RECORD
   INVALID KEY DISPLAY 'Invalid Key'
   NOT INVALID KEY DISPLAY 'Record Deleted'
END-DELETE.

示例-删除现有记录-

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
   SELECT STUDENT ASSIGN TO OUT1
      ORGANIZATION IS INDEXED
      ACCESS IS RANDOM
      RECORD KEY IS STUDENT-ID
      FILE STATUS IS FS.

DATA DIVISION.
   FILE SECTION.
   FD STUDENT
   01 STUDENT-FILE.
      05 STUDENT-ID PIC 9(4).
      05 NAME PIC A(12).
      05 CLASS PIC X(3).
   WORKING-STORAGE SECTION.
   01 WS-STUDENT.
      05 WS-STUDENT-ID PIC 9(5).
      05 WS-NAME PIC A(25).
      05 WS-CLASS PIC X(3).

PROCEDURE DIVISION.
   OPEN I-O STUDENT.
   MOVE '1000' TO STUDENT-ID.
   
   DELETE STUDENT RECORD
      INVALID KEY DISPLAY 'Invalid Key'
      NOT INVALID KEY DISPLAY 'Record Deleted'
   END-DELETE.
   
   CLOSE STUDENT.
STOP RUN.

JCL执行上述COBOL程序-

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO
//OUT1 DD DSN = OUTPUT-FILE-NAME,DISP = SHR

当您编译并执行上述程序时,它将产生以下结果-

Record Deleted

开始动词

起始动词只能在索引文件和相对文件上执行。它用于将文件指针放置在特定记录上。访问模式必须是顺序的或动态的。必须在IO或输入模式下打开文件。

句法

以下是将指针放置在特定记录的语法-

START file-name KEY IS [=, >, =] rec-key
   INVALID KEY DISPLAY 'Invalid Key'
   NOT INVALID KEY DISPLAY 'File Pointer Updated'
END-START.

关闭动词

关闭动词用于关闭文件。执行关闭操作后,文件结构中的变量将不可用于处理。程序和文件之间的链接丢失。

句法

以下是关闭文件的语法-

CLOSE file-name.