Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The general approach to this problem, reading a text file in reverse, line-wise, can be solved by at least three methods.</p> <p>The general problem is that since each line can have a different length, you can't know beforehand where each line starts in the file, nor how many of them there are. This means you need to apply some logic to the problem.</p> <h2>General approach #1: Read the entire file into memory</h2> <p>With this approach, you simply read the entire file into memory, in some data structure that subsequently allows you to process the list of lines in reverse. A stack, a doubly linked list, or even an array can do this.</p> <p><strong>Pros:</strong> Really easy to implement (probably built into Python for all I know)<br> <strong>Cons:</strong> Uses a lot of memory, can take a while to read large files</p> <h2>General approach #2: Read the entire file, store position of lines</h2> <p>With this approach, you also read through the entire file once, but instead of storing the entire file (all the text) in memory, you only store the binary positions inside the file where each line started. You can store these positions in a similar data structure as the one storing the lines in the first approach.</p> <p>Whever you want to read line X, you have to re-read the line from the file, starting at the position you stored for the start of that line.</p> <p><strong>Pros:</strong> Almost as easy to implement as the first approach<br> <strong>Cons:</strong> can take a while to read large files</p> <h2>General approach #3: Read the file in reverse, and "figure it out"</h2> <p>With this approach you will read the file block-wise or similar, from the end, and see where the ends are. You basically have a buffer, of say, 4096 bytes, and process the last line of that buffer. When your processing, which has to move one line at a time backward in that buffer, comes to the start of the buffer, you need to read another buffer worth of data, from the area before the first buffer you read, and continue processing.</p> <p>This approach is generally more complicated, because you need to handle such things as lines being broken over two buffers, and long lines could even cover more than two buffers.</p> <p>It is, however, the one that would require the least amount of memory, and for really large files, it might also be worth doing this to avoid reading through gigabytes of information first.</p> <p><strong>Pros:</strong> Uses little memory, does not require you to read the entire file first<br> <strong>Cons:</strong> Much hard to implement and get right for all corner cases</p> <hr> <p>There are numerous links on the net that shows how to do the third approach:</p> <ul> <li><a href="http://code.activestate.com/recipes/120686-read-a-text-file-backwards/" rel="noreferrer">ActiveState Recipe 120686 - Read a text file backwards</a></li> <li><a href="http://code.activestate.com/recipes/439045-read-a-text-file-backwards-yet-another-implementat/" rel="noreferrer">ActiveState Recipe 439045 - Read a text file backwards (yet another implementation)</a></li> <li><a href="http://scripts.top4download.com/read-a-text-file-backwards-/zhhvn.html" rel="noreferrer">Top4Download.com Script - Read a text file backwards</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload