Reading the entire file into an array and reversing is fine unless the file is enormous.
You could perform a buffered read of your file from back to front with something like this:
- establish a buffer_size B - this should be longer than the longest anticipated line otherwise you'll need some logic for growing the buffer size when lines are too long
- set offset = file length - buffer_size
- while the offset>=0
- read buffer_size bytes from offset
- read a line - it will be incomplete as we'll have jumped into the middle of a line, so we want to ensure the next buffer we read ends with it. Set offset = offset - buffer_size + line length
- discard that line, read all following lines into an array and reverse them
- process this array to do whatever you wanted to do