For example:
File1
AAA
BBB
CCC
DDD
File2
DDD
CCC
BBB
AAA
For example:
File1
AAA
BBB
CCC
DDD
File2
DDD
CCC
BBB
AAA
What is the logic to write a COBOL program to reverse records and move from 1 file to another?
Addressing a COBOL-only solution, the method for reversing the sequence of records in a file changed between COBOL 85 and COBOL 2002. Specifically, the REVERSED
phrase was made obsolete in COBOL 85 and removed in COBOL 2002.
COBOL 85
The following requires the input be fixed-length records with ORGANIZATION SEQUENTIAL
.
Code:
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
.
Input:
AAAABBBBCCCCDDDD
Output:
DDDDCCCCBBBBAAAA
[Note that because these are fixed-length, four-character records, there are no separators and, therefore, the records are not shown on separate lines.]
For RELATIVE
or INDEXED
files, it is necessary to, first, copy the records to a fixed-length sequential file, then use the above logic to create the "reversed" sequential file. For variable-length records, it is also necessary to save the record length as part of the fixed-length record before using the above reversing. Then, rather than writing fixed-length records, write variable-length records.
COBOL 2002 (untested)
Code:
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
.
The input file may be SEQUENTIAL
, RELATIVE
, or INDEXED
. If INDEXED
, the primary key will be used. ACCESS
must be either SEQUENTIAL
or DYNAMIC
. The records may be either fixed- or variable-length.
COBOL 2002 standard
START statement 14.8.37.3 General rules
SEQUENTIAL FILES
21) If LAST is specified, the file position indicator is set to the record number of the last existing logical record in the physical file. If no records exist in the file, or the physical file does not support the ability to position at the last record, the I-O status value in the file connector referenced by file-name-1 is set to '23', the invalid key condition exists, and the execution of the START statement is unsuccessful.
The above code, will treat the invalid key condition the same as end of file.
You should read the File1, storing the information into a Local Table. When you have all the records read, then you start writing the Local Table in the File 2, in the reverse order.