COBOL 中顺序文件、索引文件和相对文件之间的区别
文件是与特定实体相关的记录的集合。通过文件处理,我们可以按照有组织的顺序存储这些记录。这些记录存储在磁带或硬盘上。这些文件进一步分为 3 种类型:
- 顺序文件组织。
- 相对文件组织。
- 索引文件组织。
文件处理的优点:
- 它具有无限的存储空间,因此可以存储大量数据。
- 它将数据永久存储在设备上。
- 它减少了对数据的重新编辑。
文件处理的缺点:
- 它提供缓慢的访问。
- 无法有效执行操作。
用于声明文件的条目:
Python3
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT RFILE ASSIGN TO Storage Device.
[RESERVE INT-1{AREA/AREAS}]
[ORGANIZATION IS {SEQUENTIAL/RELATIVE/INDEXED}]
[ACCESS MODE IS {SEQUENTIAL/RANDOM/DYNAMIC}]
[RELATIVE KEY Variable]
[FILE STATUS IS DATA-NAME].
DATA-DIVISION.
FILE-SECTION.
FD File Name.
Python3
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE1 ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FILE1.
01 STUDENT.
02 RNO PIC 99.
02 NAME PIC A(7).
02 PERC PIC 99.99.
Python3
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE1 ASSIGN TO DISK
ORGANIZATION IS RECORD SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD LENGTH
RECORDING MODE IS V
RECORD CONTAINS 0 TO 99 CHARACTERS.
FD FILE1.
01 STUDENT.
02 RNO PIC 99.
02 NAME PIC A(7).
02 PERC PIC 99.99.
Python3
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IFILE ASSIGN TO DISK
ORGANIZATION IS INDEXED
ACCESS MODE RANDOM
RECORD KEY RNO.
DATA DIVISION.
FILE SECTION.
FD IFILE.
01 STUDENT.
02 RNO PIC 99.
02 NAME PIC A(7).
02 PERC PIC 99.99.
Python3
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT RFILE ASSIGN TO DISK
ORGANIZATION IS RELATIVE
ACCESS MODE RANDOM
RELATIVE KEY POS.
DATA DIVISION.
FILE SECTION.
FD RFILE.
01 STUDENT.
02 RNO PIC 99.
02 NAME PIC A(7).
02 PERC PIC 99.99.
顺序文件组织按顺序存储数据。我们可以顺序访问数据,并且数据只能存储在文件末尾。有两种类型的顺序文件:
- 行顺序文件也称为文本文件或 ASCII 文件。它是一个简单的文本文件,几乎所有 PC 编辑器都可以编辑。它通过在记录末尾添加分隔符来将每条记录与其他记录分开。在 windows 和 DOS 的情况下,回车 ( x"OD" ) 和换行( x”OA” ) 在记录的末尾添加,而在 UNIX 中只在记录的末尾添加换行符(x”OA” )。
例子:
蟒蛇3
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE1 ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FILE1.
01 STUDENT.
02 RNO PIC 99.
02 NAME PIC A(7).
02 PERC PIC 99.99.
- 记录顺序文件是默认的顺序文件。这些记录要么基于程序员定义的大小(字节),要么基于记录的大小。
- 如果记录的大小由程序员定义,则称为固定长度。
- 如果记录是根据记录的大小存储的,那么它被称为可变长度。
与定长记录相比,变长记录可以节省硬盘空间。
例子:
蟒蛇3
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE1 ASSIGN TO DISK
ORGANIZATION IS RECORD SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD LENGTH
RECORDING MODE IS V
RECORD CONTAINS 0 TO 99 CHARACTERS.
FD FILE1.
01 STUDENT.
02 RNO PIC 99.
02 NAME PIC A(7).
02 PERC PIC 99.99.
索引文件组织根据 RECORD-KEY 的值顺序存储记录(通常按升序)。索引文件中的 RECORD-KEY 是一个变量,它必须是记录/数据的一部分。在索引文件的情况下,会创建两种类型的文件:
- 数据文件:它由按顺序排列的记录组成。
- 索引文件:由RECORD-KEY和RECORD-KEY在数据文件中的地址组成。
索引文件可以像顺序文件组织一样顺序访问,也可以随机访问,前提是记录密钥是已知的。
例子:
蟒蛇3
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IFILE ASSIGN TO DISK
ORGANIZATION IS INDEXED
ACCESS MODE RANDOM
RECORD KEY RNO.
DATA DIVISION.
FILE SECTION.
FD IFILE.
01 STUDENT.
02 RNO PIC 99.
02 NAME PIC A(7).
02 PERC PIC 99.99.
相对文件组织根据其相对地址存储记录。每条记录由其相对记录号标识,相对记录号是记录从文件开头的位置。这些记录可以像顺序文件组织一样顺序访问,也可以随机访问,要随机访问文件,用户必须指定相关的记录号。
例子:
蟒蛇3
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT RFILE ASSIGN TO DISK
ORGANIZATION IS RELATIVE
ACCESS MODE RANDOM
RELATIVE KEY POS.
DATA DIVISION.
FILE SECTION.
FD RFILE.
01 STUDENT.
02 RNO PIC 99.
02 NAME PIC A(7).
02 PERC PIC 99.99.
顺序、索引、相对文件之间的区别: Sequential files Indexed files Relative files These files can be accessed only sequentially. These files can be accessed sequentially as well as randomly with the help of the record key. These files can be accessed sequentially as well as randomly with the help of their relative record number. The records are stored sequentially. The records are stored based on the value of the RECORD-KEY which is the part of the data. The records are stored by their relative address. Records cannot be deleted and can only be stored at the end of the file. It is possible to store the records in the middle of the file. The records can be inserted at any given position. It occupies less space as the records are stored in continuous order. It occupies more space. It occupies more space. It provides slow access, as in order to access any record all the previous records are to be accessed first. It also provides slow access(but is fast as compared to sequential access) as it takes time to search for the index. It provides fast access as provides the record key compared to the other two. In Sequential file organization, the records are read and written in sequential order. In Indexed file organization, the records are written in sequential order but can be read in sequential as well as random order. In Relative file organization, the records can be written and read in sequential as well as random order. There is no need to declare any KEY for storing and accessing the records. One or more KEYS can be created for storing and accessing the records. Only one unique KEY is declared for storing and accessing the records.