read directly the last record of a input file

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
Vibha
Registered Member
Posts: 44
Joined: Tue Jul 16, 2013 7:35 pm
India

read directly the last record of a input file

Post by Vibha »

Hi,

I have a requirement to read directly the last record of a input file. How can i do it?
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1903
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: read directly the last record of a input file

Post by Robert Sample »

If the input file is sequential, you cannot directly read the last record.
Vibha
Registered Member
Posts: 44
Joined: Tue Jul 16, 2013 7:35 pm
India

Re: read directly the last record of a input file

Post by Vibha »

Thanks. Then what is best way to do it? Please advise.
BobP
Registered Member
Posts: 55
Joined: Mon Jun 17, 2013 1:33 pm
United States of America

Re: read directly the last record of a input file

Post by BobP »

SORT in reverse, prior to reading, that might do it.
Thanks,
User avatar
zum13
Active Member
Posts: 127
Joined: Thu May 04, 2023 12:58 am

Re: read directly the last record of a input file

Post by zum13 »

Sorting in reverse presumes that there is a key on the file which can be used with 100% accuracy to reverse the order of the file, which there may not be. You'd also be not only reading but writing the whole file to do it, so it's a bit of a waste of resources.

The reason why reading the entirety of a file to get the last record is required is down to device compatibility. Older devices, such as paper tape or punch cards, and tape are designed to be read sequentially because of their physical nature. The DDNames which are used to present files to programs can potentially use data held on any compatible device, and they are also designed to allow you to change the input to your program without you having to modify your code. This necessitates an API which is compatible across devices for sequential datasets. Even though DASD supports random access, tape or those older devices won't, therefore the part of the API which handles sequential file access doesn't let you do it.

The only way to get the last record of a sequential file is to read to the end.
BobP
Registered Member
Posts: 55
Joined: Mon Jun 17, 2013 1:33 pm
United States of America

Re: read directly the last record of a input file

Post by BobP »

That's true but I wonder what business requirement really makes to read file from last record?
Thanks,
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”