There are two three four potential approaches to this problem, but as it mostly sounds like homework to me, I'll only outline the general approaches:
The first approach is to observe that reverse(A+B) = reverse(B) + reverse(A), so you can split your long list into many smaller lists, reverse these smaller lists and then join the reversed lists back together. If you want to do this in an recursive approach, you could also observe that reverse(a) == a and treat that as a stopping condition, although that would be quite inefficient, as would be the observation that reverse(a+TAIL) == reverse(TAIL)+a.
The second approach, which works as long as you have enough core memory for storing a list of numbers, would be to read through the whole file, noting the offset of each line (as told via tell), and then to reverse that list of numbers, seek to each offset in the file and to then write out the line from there into the new file. If you are careful with your memory usage, or if you are tricky and store your offsets packed in a string, for example as 4-byte or 8-byte tuples, or maybe even as just the (presumed shorter) list of line-lengths, you can reduce the amount of memory you need to store the information and still reverse the whole file.
A third approach would be to not store any information and just seek a bit backwards from the current location and read data up to the current location, then scan for a line break. If one is found, output the line from there. Seek further backwards.
A fourth approach would be to use File::ReadBackwards.
In reply to Re: reversing files line by line
by Corion
in thread reversing files line by line
by naturalsciences
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |