Solved

Reading text file backwards

Posted on 1999-01-06
4
672 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 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