Solved

Reading text file backwards

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month7 days, 9 hours left to enroll

632 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