DelphiFAQ Home Search:

Read a text file backwards

 

comments4 comments. Current rating: 4 stars (4 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);
const
  MAXLINELENGTH = 256;
var
  curr,
  Before : Longint;
  Buffer : array [0..MAXLINELENGTH] of char;
  p      : PChar;
begin
  // 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
  begin
    Line := StrPas (Buffer);
    FileSeek (f, 0, 0);
    _bof := True
  end
  else
  begin
    Line := StrPas (p + 1);
    FileSeek (f, Before + Longint (p) - Longint (@Buffer), 0);
    _bof := False
  end;

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


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

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

  FileClose (f);
end;

Comments:

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

This source help me with my work.
Thank's.

God bless you !

Keywords:

2007-09-22, 09:35:16
anonymous from Tokyo, Japan  
rating
great !!
2007-09-30, 19:24:26
anonymous  
rating
Wow!!! Good job. Could I take some of yours triks to build my own site?f
2008-03-24, 09:37:29
anonymous  
rating
<a href= http://eumao.info/m..a-ringtone >motorola-ringtone</a>
keep up the good work!
<a href= http://ecenj.info/d..movie-porn >downloadable-free-movie-porn</a>
<a href= http://eehf.org/fre..shing-game >free-online-fishing-game</a>
<a href= http://eehf.org/asi..male-movie >asian-shemale-movie</a>
<a href= http://eumsg.info/j..lry-to-buy >jewelry-to-buy</a>
l

 

 

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.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
Leave your comment here:
Please type in the code:
photo Add a picture:

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.