Solved

Reading text file backwards

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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