📜  COBOL 中的文件处理动词

📅  最后修改于: 2022-05-13 01:54:37.201000             🧑  作者: Mango

COBOL 中的文件处理动词

属于同一实体的记录集合称为File 。这些记录被永久保存。文件处理有助于按升序/降序组织这些记录。它使搜索、访问这些记录更加容易和高效。记录通常存储在磁带或磁盘上。

文件处理的优点:

  1. 它具有无限的存储空间,因此可以存储大量数据。
  2. 它将数据永久存储在设备上。
  3. 它减少了数据的重新编辑。

文件处理的缺点:

  1. 它提供缓慢的访问。
  2. 无法高效执行操作。

COBOL 中的文件处理动词有助于对文件执行不同的所需操作。这些动词是:

  1. 打开
  2. 关闭
  3. 改写
  4. 开始
  5. 删除

下面是这些文件处理动词的详细描述。

  • OPEN: OPEN 动词打开文件以对其执行进一步的操作,即它使文件可用于执行任何操作。不打开文件就不能执行任何操作,因此这必须是对文件执行的第一个操作。有 4 种模式可以打开文件:
    • 输入模式:它有助于从文件中读取记录/数据。
    • 输出模式:有助于将记录/数据写入文件。
    • EXTEND 模式:它有助于在文件末尾写入新的记录/数据,即它不会删除文件以前的记录,这与 OUTPUT 模式不同。
    • IO 模式:它以 INPUT 和 OUTPUT 模式打开文件。

句法:

OPEN {INPUT/OUTPUT/EXTEND/I-O} file_name_1[,file_name_2,...].

例子:

Cobol
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
         SELECT FILE1 ASSIGN TO DISK
         ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
       FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
        77 CHOICE PIC 9.
        77 EOF PIC 9.
PROCEDURE DIVISION.
    OPEN OUTPUT FILE1.
    DISPLAY "FILE1 OPENED".
    CLOSE FILE1.
    STOP RUN.


Cobol
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
         SELECT FILE1 ASSIGN TO DISK
         ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
       FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
        77 CHOICE PIC 9.
        77 EOF PIC 9.
PROCEDURE DIVISION.
    OPEN OUTPUT FILE1.
    DISPLAY "FILE1 OPENED".
    CLOSE FILE1.
    DISPLAY "FILE1 CLOSED".
    STOP RUN.


Cobol
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN INPUT FILE1.
      PERFORM W-PARA UNTIL EOF = 1.
      CLOSE FILE1.
      STOP RUN.
        
      W-PARA.
             READ FILE1 AT END MOVE 1 TO EOF.
           IF EOF = 0
                   DISPLAY "RNO: ",RNO,"NAME: ",NAME,"PERCENTAGE: ",PERC.


Cobol
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN OUTPUT FILE1.
      MOVE 1 TO RNO.
      MOVE 'XYZ' TO NAME.
      MOVE 34.56 TO PERC.
      WRITE STUDENT.
      CLOSE FILE1.
      STOP RUN.


Cobol
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN I-O FILE1.
      READ FILE1 AT END MOVE 1 TO EOF.
          IF EOF = 0
              IF RNO = 5 
                  MOVE 'XYZ' TO NAME
                  REWRITE STUDENT.
      CLOSE FILE1.
      STOP RUN.


Cobol
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION IS INDEXED.
              ACCESS MODE IS DYNAMIC.
              RECORD-KEY IS RNO.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN I-O FILE1.
      START FILE1 KEY IS NOT LESS THAN 5
          INVALID KEY DISPLAY " WRONG KEY".
      CLOSE FILE1.
      STOP RUN.


Cobol
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION IS INDEXED.
              ACCESS MODE IS DYNAMIC.
              RECORD-KEY IS RNO.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN I-O FILE1.
      MOVE 5 TO RNO.
      DELETE FILE1 
          INVALID KEY DISPLAY " WRONG KEY".
      CLOSE FILE1.
      STOP RUN.


  • CLOSE:执行完所有操作后应该关闭文件。 CLOSE 动词禁用文件和程序之间的链接。执行关闭操作后,文件变量将不再可用于执行该操作。

句法:

CLOSE file_name_1[,file_name_2,....].

例子:

科博尔

IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
    FILE-CONTROL.
         SELECT FILE1 ASSIGN TO DISK
         ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
       FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
        77 CHOICE PIC 9.
        77 EOF PIC 9.
PROCEDURE DIVISION.
    OPEN OUTPUT FILE1.
    DISPLAY "FILE1 OPENED".
    CLOSE FILE1.
    DISPLAY "FILE1 CLOSED".
    STOP RUN.
  • READ: READ 动词允许读取文件的记录。一次,只能将一条记录读入文件结构,并且在读取一条记录后,文件指针加一。要读取记录,文件必须以 INPUT 模式或 IO 模式打开。一旦文件指针到达文件末尾,就执行写在“AT END”子句中的命令式语句。

句法:

READ file-name RECORD [INTO identifier-1] AT END imperative statement. 

例子:

科博尔

IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN INPUT FILE1.
      PERFORM W-PARA UNTIL EOF = 1.
      CLOSE FILE1.
      STOP RUN.
        
      W-PARA.
             READ FILE1 AT END MOVE 1 TO EOF.
           IF EOF = 0
                   DISPLAY "RNO: ",RNO,"NAME: ",NAME,"PERCENTAGE: ",PERC.
  • WRITE: WRITE 动词允许将记录写入文件。一次只能将一条记录写入文件结构,并且在写入一条记录后,文件指针会增加 1,因此这些记录是一个接一个地写入的。要将记录写入文件,文件必须以 OUTPUT 模式或 IO 模式打开。如果我们想将 identifier-1 的值写入文件,则执行“FROM”子句。

句法:

WRITE record-name [ FROM identifier-1].

例子:

科博尔

IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN OUTPUT FILE1.
      MOVE 1 TO RNO.
      MOVE 'XYZ' TO NAME.
      MOVE 34.56 TO PERC.
      WRITE STUDENT.
      CLOSE FILE1.
      STOP RUN.
  • REWRITE: REWRITE 动词有助于更新现有记录,即如果用户想要重写或对现有记录进行任何更改,则使用 REWRITE 动词。要重写记录,文件必须以 IO 模式打开。如果用户想要将 identifier-1 的值写入文件,则执行“FROM”子句。

句法:

REWRITE record-name [FROM identifier-1].

例子:

科博尔

IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN I-O FILE1.
      READ FILE1 AT END MOVE 1 TO EOF.
          IF EOF = 0
              IF RNO = 5 
                  MOVE 'XYZ' TO NAME
                  REWRITE STUDENT.
      CLOSE FILE1.
      STOP RUN.
  • START:如果用户想从特定位置开始读取记录,则使用 START 动词。文件必须以 IO 模式打开,文件的访问模式必须是 SEQUENTIAL 或 DYNAMIC。如果任何键都不满足比较或从未定义的位置访问文件,则执行“INVALID KEY”子句。

句法:

START file-name [KEY IS {EQUAL TO/ = /GREATER THAN/ > /NOT LESS THAN/NOT < THAN} data-name] [INVALID KEY imperative statement].

例子:

科博尔

IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION IS INDEXED.
              ACCESS MODE IS DYNAMIC.
              RECORD-KEY IS RNO.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN I-O FILE1.
      START FILE1 KEY IS NOT LESS THAN 5
          INVALID KEY DISPLAY " WRONG KEY".
      CLOSE FILE1.
      STOP RUN.
  • DELETE:要从文件中删除任何记录,请使用 DELETE 动词。该文件必须以 IO 模式打开。如果文件的访问模式是 SEQUENTIAL,则不应指定 INVALID KEY 短语,并且 DELETE 动词必须以文件上的 READ 语句开头。如果用户试图删除文件中不存在的记录,则执行“INVALID KEY”子句。

句法:

DELETE file-name [ INVALID KEY imperative statement]. 

例子:

科博尔

IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE1 ASSIGN TO DISK
             ORGANIZATION IS INDEXED.
              ACCESS MODE IS DYNAMIC.
              RECORD-KEY IS RNO.
DATA DIVISION.
    FILE SECTION.
        FD FILE1.
           01 STUDENT.
             02 RNO PIC 99.
             02 NAME PIC A(7).
             02 PERC PIC 99.99.
    WORKING-STORAGE SECTION.
           77 CHOICE PIC 9.
           77 EOF PIC 9.
PROCEDURE DIVISION.
      OPEN I-O FILE1.
      MOVE 5 TO RNO.
      DELETE FILE1 
          INVALID KEY DISPLAY " WRONG KEY".
      CLOSE FILE1.
      STOP RUN.