DelphiFAQ Home Search:

Read a text file backwards


comments10 comments. Current rating: 4 stars (5 votes). Leave comments and/ or rate it.

To read a text file backwards, you have to open it as a binary file, e.g. with FileOpen().
The following procedure ReadBack() reads one line backwards - up to the current position -.
So, initially, you need to position to the end of the file.

The usage is demonstrated in the routine FormCreate() below:

procedure Readback (const f: integer; var Line: String; var _bof: boolean);
  Before : Longint;
  Buffer : array [0..MAXLINELENGTH] of char;
  p      : PChar;
  // scan backwards to the last CR-LF
  curr := FileSeek (f, 0, 1);
  Before := curr - MAXLINELENGTH;
  if Before < 0 then
    Before := 0;
  FileSeek (f, Before, 0);
  FileRead (f, Buffer, curr - Before);
  Buffer[curr - Before] := #0;
  p := StrRScan (Buffer, #10);
  if p = Nil then
    Line := StrPas (Buffer);
    FileSeek (f, 0, 0);
    _bof := True
    Line := StrPas (p + 1);
    FileSeek (f, Before + Longint (p) - Longint (@Buffer), 0);
    _bof := False

  // this will also work with Unix files (#10 only, no #13)
  if length (Line) > 0 then
    if Line[length (Line)] = #13 then
      SetLength (Line, length (Line) - 1)

procedure TForm1.FormCreate (Sender: TObject);
  FileName = 'c:\delphi3\bin\unit1.pas';
  f           : integer;
  Line        : string;
  BeginOfFile : boolean;
  f := FileOpen (FileName, 0);
  // move to end of file!
  FileSeek (f, 0, 2);

  // read all lines, backwards
    Readback (f, Line, BeginOfFile);
    ListBox1.Items.Insert (0, Line);
  until BeginOfFile;

  FileClose (f);

Content-type: text/html


2007-08-17, 06:01:52   (updated: 2007-08-17, 06:02:55)
elrictheviking from Brazil  
Great !

This source help me with my work.

God bless you !

2007-09-22, 09:35:16
anonymous from Tokyo, Japan  
great !!
2007-09-30, 19:24:26
Wow!!! Good job. Could I take some of yours triks to build my own site?f
2008-07-16, 11:01:45
Many interesting information on your site - keep up good works
2012-10-23, 01:48:42
megaabstauberseo from Philippines  
I certainly enjoyed the way you explore your experience and knowledge of the subject! Keep up on it. Thanks for sharing the info.
2012-11-06, 04:47:10
anonymous from Pakistan  
It should be noted that whilst ordering papers for sale at paper writing service, you can get unkind attitude. In case you feel that the bureau is trying to cheat you, don't buy term paper from it.     <a href=';wedding up dress</a>
2012-11-28, 01:05:41
anonymous from India  
For, after all, put it as we may to ourselves, we are all of us from birth to death guests at a table which we did not spread. The sun, the earth, love, friends, our very breath are parts of the banquet.... Shall we think of the day as a chance to come nearer to our Host, and to find out something of Him who has fed us so long? ~Rebecca Harding Davis http://rajasthanisp..rees.html
2013-03-27, 07:07:02
[hidden] from La Habana, Cuba  
Thanks a lot; your post was helpful to me...
2013-06-13, 10:53:50
anonymous from Pakistan  
Very interesting looking post and I must appraise your efforts to write this post.
<a href='' title='Paper Writing Service'>Paper Writing Service</a>
2013-07-15, 01:50:26
anonymous from United Kingdom  
As our acculturation and Economy grows and becomes mature, the accountability on the Alley and added cartage arrangement increases day by day. Hence, it is

actual important to clean and Robust our cartage basement for abiding and accelerating advance in every field. So,
<a href=' http://www.idealobd..'>lexia 3</a>we charge the latest abstruse Discoveries

and Cartage ascendancy accessories acceptable for the Indian cartage activity and its Road.



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: