Solved

Reading text file backwards

Posted on 1999-01-06
4
627 Views
Last Modified: 2010-05-18
Is there to read a text file backwards?
I know u can read it foward sequentially....
Read or Realn
but how do u read it backwards....

also, anybody have any clue on getting the novell user login
id..i posted this on another question...did the
WNETGETUser but it is unstable..sometimes it works
and other times it doesnt...
thanks a lot!
0
Comment
Question by:waipahu
  • 2
4 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1355028
The easiest way is like this:

var
  TheFile : TStringList;
  i : integer;

begin
  TheFile := TStringList.Create;
  TheFile.LoadFromFile('FileName.txt');
  for I := TheFile.lines.count - 1 downto 0 do
    begin
      //Do stuff with TheFile.lines[i]...
    end;
  TheFile.Free;
end;

Cheers,

Raymond.  
0
 

Author Comment

by:waipahu
ID: 1355029
Thanks Wilson...
Your solution will work with a lot of situations...however, i dont think
it will work when i have a gigantic text file..
i wanted to read a page at a time....
i guess i'm more concerned about trying to keep track of where the pointer
is pointing to....i am trying to write a program to read a report that was
generated on a UNIX machine..each page on the report has a form feed chr(12) or #12
i want to read a page at a time until i hit the form feed character...
when the user pressed page down i want to read the next page starting from
the form feed character until the next form feed character - 1 line. if the user
pressed page up then i want to read the previous page...possible reading backwards
or repositioning the pointer and read from that point on.

make any sense?

Thanks again!!!!!

p.s.  some reports may be over a thousand pages in size....thats why i cant
read it all one time.....
0
 

Accepted Solution

by:
ptiemann2 earned 10 total points
ID: 1355030
you have to use something like the following procedure ReadBack(), the second proc FormCreate() shows how to use it in a loop (read until "begin-of-file".

have fun.. Peter


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;
  begin_of_file : boolean;
Begin
  f := FileOpen (FileName, 0);
  // move to end of file!
  FileSeek (f, 0, 2);
 
  Repeat
    Readback (f, Line, begin_of_file);
    ListBox1.Items.Insert (0, Line);
  Until begin_of_file;
 
  FileClose (f)
End;


0
 

Expert Comment

by:ptiemann2
ID: 1355031
I forgot something.. make sure that the constant MAXLINELENGTH is appropriate..

and yes.. you can use this in a unix environment .. and if you want to read a page at a time, exchange the #10 with a #12 - that's all!

Peter (ptiemann2)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to use Hashing sha1 in Delphi 2010 4 206
Sending Gmail through Delphi 3 91
Delphi: how to send PJL commands to printer 3 90
Adoquery sql  left join does not work 25 80
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now