编写COBOL程序以反转记录并从一个文件移动到另一个文件的逻辑是什么?

问题描述 投票:-1回答:1

例如:

File1

AAA
BBB
CCC
DDD

File2

DDD
CCC
BBB
AAA
cobol mainframe
1个回答
0
投票

编写COBOL程序以反转记录并将其从一个文件移动到另一个文件的逻辑是什么?

解决了仅使用COBOL的解决方案,用于反转文件中记录顺序的方法在COBOL 85和COBOL 2002之间更改。具体地说,REVERSED短语在COBOL 85中已作废,而在COBOL 2002中已删除。


COBOL 85

以下要求输入的内容必须是带有ORGANIZATION SEQUENTIAL的定长记录。

代码:

   environment division.
   input-output section.
   file-control.
       select file1 assign "file1.dat"
           organization sequential
       .
       select file2 assign "file2.dat"
           organization sequential
       .
   data division.
   file section.
   fd file1.
   01 file1-rec pic x(4).
   fd file2.
   01 file2-rec pic x(4).
   working-storage section.
   01 eof-flag pic 9 value 0.
     88 eof-file1 value 1.
   procedure division.
   begin.
       open input file1 reversed
           output file2
       perform read-file1
       perform until eof-file1
           write file2-rec from file1-rec
           perform read-file1
       end-perform
       close file1 file2
       stop run
       .

   read-file1.
       read file1
       at end
           set eof-file1 to true
       end-read
       .

输入:

AAAABBBBCCCCDDDD

输出:

DDDDCCCCBBBBAAAA

[请注意,由于这些记录是固定长度的四个字符的记录,因此没有分隔符,因此这些记录不会显示在单独的行上。]

对于RELATIVEINDEXED文件,有必要首先将记录复制到固定长度的顺序文件中,然后使用上述逻辑来创建“反向”顺序文件。对于可变长度记录,在使用上述反转之前,还必须将记录长度保存为固定长度记录的一部分。然后,不要写定长记录,而要写变长记录。


COBOL 2002(未试用)

代码:

   environment division.
   input-output section.
   file-control.
       select file1 assign "file1.dat"
           organization sequential
       .
       select file2 assign "file2.dat"
           organization sequential
       .
   data division.
   file section.
   fd file1.
   01 file1-rec pic x(4).
   fd file2.
   01 file2-rec pic x(4).
   working-storage section.
   01 eof-flag pic 9 value 0.
     88 eof-file1 value 1.
   procedure division.
   begin.
       open input file1
           output file2
       start file1 last
       invalid key
           set eof-file1 to true
       not invalid key
           perform read-file1
       end-start
       perform until eof-file1
           write file2-rec from file1-rec
           perform read-file1
       end-perform
       close file1 file2
       stop run
       .

   read-file1.
       read file1 previous
       at end
           set eof-file1 to true
       end-read
       .

输入文件可以是SEQUENTIALRELATIVEINDEXED。如果为INDEXED,则将使用主键。 ACCESS必须为SEQUENTIALDYNAMIC。记录可以是固定长度的,也可以是可变长度的。

COBOL 2002标准

START陈述14.8.37.3一般规则

顺序文件

21)如果指定了LAST,则文件位置指示符将设置为物理文件中最后一个现有逻辑记录的记录号。如果文件中不存在任何记录,或者物理文件不支持定位到最后一条记录,则文件名-1所引用的文件连接器中的IO状态值将设置为'23',即无效键条件存在,并且START语句的执行失败。

上面的代码,将无效键条件与文件末尾相同。

© www.soinside.com 2019 - 2024. All rights reserved.