Solved

Reading text file backwards

Posted on 1999-01-06
4
616 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

10 Experts available now in Live!

Get 1:1 Help Now