Assuming you don't mind that the file gets read several times over, this is the way I would do it. It ought to be fairly efficient.
In a first pass, you must get an array of offsets of where lines start in the file. However, you don't need the offset for every single line.
Divide the file into chunks of reasonable size. For example, if you allow at most about 500k from the file in memory at one, first seek in the file with a value close to 500_000, read one (incomplete) line, call tell on the handle, and store it in an array. Add 500_000 to that offset, and repeat, until you get past the file size. You end up with an array of indexes of where lines start. Note that the first value in the array must be 0; the last value ought to be the length of the file.
Loop through that array backwards, each time getting one offset, and the next offset. seek to the lower value; read a block of as many bytes as the difference between the two. Reverse the lines in that block, and write it to a new file ,each time appending to what you wrote there last time.
In reply to Re: reversing files line by line
by bart
in thread reversing files line by line
by naturalsciences
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |