Page 1 of 1

read directly the last record of a input file

Posted: Wed Jul 24, 2024 11:40 am
by Vibha
Hi,

I have a requirement to read directly the last record of a input file. How can i do it?

Re: read directly the last record of a input file

Posted: Wed Jul 24, 2024 7:00 pm
by Robert Sample
If the input file is sequential, you cannot directly read the last record.

Re: read directly the last record of a input file

Posted: Thu Jul 25, 2024 5:31 pm
by Vibha
Thanks. Then what is best way to do it? Please advise.

Re: read directly the last record of a input file

Posted: Mon Sep 30, 2024 10:30 am
by BobP
SORT in reverse, prior to reading, that might do it.

Re: read directly the last record of a input file

Posted: Mon Sep 30, 2024 7:40 pm
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.

Re: read directly the last record of a input file

Posted: Thu Oct 03, 2024 10:24 am
by BobP
That's true but I wonder what business requirement really makes to read file from last record?