Hi,
I have a requirement to read directly the last record of a input file. How can i do it?
read directly the last record of a input file
- Robert Sample
- Global Moderator
- Posts: 1903
- Joined: Fri Jun 28, 2013 1:22 am
- Location: Dubuque Iowa
Re: read directly the last record of a input file
If the input file is sequential, you cannot directly read the last record.
Re: read directly the last record of a input file
Thanks. Then what is best way to do it? Please advise.
Re: read directly the last record of a input file
SORT in reverse, prior to reading, that might do it.
Thanks,
Re: read directly the last record of a input file
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.
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
That's true but I wonder what business requirement really makes to read file from last record?
Thanks,
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