How to read line by line from text file

But itch reading has to read only on line and i'll star it from a timer.

And first reading has to read first line,
second reading has to read second line
and so one. Every reading has to update specific variable.

For read all lines in the TMemo am using next code (attached)
What changes must i do to my code to make it work in described way  
//read note to memo
procedure TForm2.read(Ime:string);
begin
   Memo1.Lines.LoadFromFile(Ime);
  Memo1.SelStart := Length(Memo1.Lines.Text) - 1;
  Memo1.SelLength := 1;
  Memo1.SetFocus;
  end;

Open in new window

pr2501Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ziolkoCommented:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    Edit1: TEdit;
    Edit2: TEdit;
    Button3: TButton;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    FFile: TStringList;
    FCurrentLine: Integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
//
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Timer1.Enabled := not Timer1.Enabled;
  FCurrentLine := 0;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FFile := TStringList.Create;
  FFile.LoadFromFile('c:\boot.ini');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FFile.Free;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if FCurrentLine < FFile.Count then begin
    Memo1.Lines.Add(FFile[FCurrentLine]);
    Inc(FCurrentLine);
  end else begin
    Memo1.Lines.Add('end of file');
    Timer1.Enabled := False;
  end;
end;

end.

ziolko.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark BradyPrincipal Data EngineerCommented:
I am not clear on what you want to achieve.  More information is needed.  Do you want to load a tmemo from a text file then have a timer read the memo lines one by one and make changes ?
If so then this will work.

place a label on your form and set the caption to '1'

procedure TForm1.FormCreate(Sender: TObject);
var
yourfile : String;
begin
yourfile := 'C:\yourfile.txt';   // rename this to your actual filename
if fileexists (yourfile) then
memo1.lines.loadfromfile(yourfile);
end;

// now memo1 is loaded with the lines from the text file.

On the Timer1.timer use something like this

var
s: string;
i: integer;
begin
i := strtoint(label1.caption);
s := memo1.lines.text[i];
// Do something with the string here for eg:
s:= s + 'This line has been modified';
memo1.lines[i] := s;
label1.caption := (inttostr(strtoint(label1.caption + 1)));
if strtoint(label1.caption) > memo1.lines.count then
timer1.timer := false;
end;




0
tobjectpascalCommented:
Here's my Entry.  place 1 button, 1 memo and 1 timer and use the code below.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Memo1: TMemo;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  T: TextFile;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
 Timer1.Enabled:=False; //Disbale the timer;
 Timer1.Interval:=1000; //1 second reads
 System.Assign(T,'c:\getrva.txt'); //open the text file
 System.Reset(T); //Open it to read
                                       //Timer1.Enabled:=True; //Start Reading Text File put this anywhere...
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 System.Close(T); //Close the file handle;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
Var
 Str: String;
begin
 If Not Eof(T) Then
   Begin
     Readln(T,Str);       //read 1 line
     Memo1.Lines.Add(Str); //display it to memo control
   End;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 Timer1.Enabled:=True; //Start Reading Text File
end;

end.
0
pr2501Author Commented:


  thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.