-2

For example:

File1

AAA
BBB
CCC
DDD

File2

DDD
CCC
BBB
AAA
Martin
  • 16,093
  • 1
  • 29
  • 48
Srikanth Sri
  • 1
  • 1
  • 1
  • 1
    Hi and welcome to SO. Currently this is not a good question for here as it is not about a specific programming problem. Please include the code you have already written and describe what it is supposed to do and what it is actually doing. – Steve Ives Dec 16 '19 at 10:59
  • How many records? You show 4 and if this was the case you could read them into a table and write from the table in reverse order. If your actual record count is thousands or millions then don't bother. Use your sort product. – NicC Dec 16 '19 at 11:07

2 Answers2

3

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.

Rick Smith
  • 3,962
  • 6
  • 13
  • 24
  • I *guess* it likely works for all file types but `LINE SEQUENTIAL` (which may be no issue on the mainframe). Wouldn't `SORT` be even better (or is this not supported on the mainframe with any file type)? – Simon Sobisch Dec 16 '19 at 17:53
  • @SimonSobisch - `LINE SEQUENTIAL` has, effectively, variable-length records. For COBOL 85, it could be treated like any other file with variable-length records; that is, copy to a temporary fixed-length file, reverse it writing the output to another line sequential file. As for `SORT` on a mainframe, if the file is other than fixed-length record sequential, sort may well be faster; but it does multiple reads, writes, and compares, while `REVERSE` is one read, one write. [I haven't done anything on a mainframe in 40 years.] – Rick Smith Dec 16 '19 at 18:26
  • Okay, I had the similar code and I tried your code again, it gives me the same output which I got. File 1 AAAA BBBB CCCC DDDD FILE2 AAAA BBBB CCCC DDDD It is not reversing the records in file but just copying. – Srikanth Sri Dec 16 '19 at 18:56
  • 1
    @SrikanthSri - Edit your code in the question so we can see what may be wrong, or provide other possible solutions. – Rick Smith Dec 16 '19 at 18:59
0

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.