Link to home
Start Free TrialLog in
Avatar of itektas
itektas

asked on

displaying text file content to a Tmemo

 I am currently trying to write a program that reads the contents of a text file to display it into a Tmemo, i have written the following code, it does not compile! help please

procedure ReadFromFile(Memo1 :TMemo; '/root/test.txt':String);

var

OpenFile : TextFile;
Outstring: string;

begin

Memo1.Lines.Clear;
AssignFile(OpenFile, '/root/test.txt');
Reset(OpenFile);
ReadLn(OpenFile, Outstring);
Memo1.Lines.Add(Outstring);
CloseFile(OpenFile);
end;
end.

The error that i get is  at the first line " identifier expected but string constant found"  I Have tried some modifications on the program like  removing"; '/root/test.txt':String" which makes it compile successfully, but then when i run the program it does not read and display the contents of the text file in the Tmemo. what am i doing wrong? And i could I change this program for it to display the contents into edit boxes. Thanks
Avatar of arjanh
arjanh

You can simply import a text file into a TMemo as follows:
Memo1.Lines.LoadFromFile('/root/test.txt');
Avatar of itektas

ASKER

I will try that now, but where in my code does it go?
And line 1 sould read something like
procedure ReadFromFile(Memo1 :TMemo; FileName:String);

Within that procedure you then use
Memo1.Lines.Clear;
AssignFile(OpenFile, FileName );
Reset( OpenFile);
while not Eof( OpenFile ) do
begin
  ReadLn(OpenFile, Outstring);
  Memo1.Lines.Add(Outstring);
end;
CloseFile(OpenFile);

When you call ReadFromFile in your main program, you have to give the filename:
ReadFromFile( myMemo, '/root/test.txt' );
My earlier sugestion would become

procedure ReadFromFile(Memo1 :TMemo; FileName:String);
begin
  Memo1.Lines.Clear;
  Memo1.Lines.LoadFromFile( FileName );
end;

and again, somewhere in your program you would do
ReadFromFile( myMemo, '/root/test.txt' );
Avatar of itektas

ASKER

when i do the following

procedure ReadFromFile (Memo1:TMemo; '/root/test.txt' :String);
begin
Memo1.Lines.Clear;
Memo1.Lines.LoadFromFile('/root/test.txt');
end;

the compiler doenst seem to like the first line at all! it keeps saying identifier expected but string constant found. I'm also a bit confused when you say that I need to include ReadFromFile(myMemo, '/root/test.txt'); Does that need to go somewhere else besides the implementation of the program? The same thing happens when i try the first program you told me, it doenst like the first line and says the same error ? thanks
ASKER CERTIFIED SOLUTION
Avatar of arjanh
arjanh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of itektas

ASKER

ok it works now!! I had to change one thing tho, whenever i put the filename into the procedure it would not compile, but it works when i remove it.

procedure  ReadFromFile (Memo1 :TMemo);
begin
Memo1.Lines.Clear;
Memo1.Lines.LoadFromFile('/root/test.txt');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ReadFromFile( Memo1);

thanks for that mate,
This was my code a whle ago ...

procedure button1.click (TSender: TObject)
begin
memo1.Loadfromfile ('c:\text.txt')
end;