Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

displaying text file content to a Tmemo

Posted on 2003-11-03
8
Medium Priority
?
506 Views
Last Modified: 2010-04-05
 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
0
Comment
Question by:itektas
[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
  • 4
  • 3
8 Comments
 
LVL 5

Expert Comment

by:arjanh
ID: 9674334
You can simply import a text file into a TMemo as follows:
Memo1.Lines.LoadFromFile('/root/test.txt');
0
 

Author Comment

by:itektas
ID: 9674382
I will try that now, but where in my code does it go?
0
 
LVL 5

Expert Comment

by:arjanh
ID: 9674383
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' );
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Expert Comment

by:arjanh
ID: 9674402
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' );
0
 

Author Comment

by:itektas
ID: 9674524
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
0
 
LVL 5

Accepted Solution

by:
arjanh earned 80 total points
ID: 9674624
There are two things: you _define_ a procedure, and you _call_ it from somewhere.

The procedure is like a recipe: do something with the given ingredients (in this case a TMemo and a String). This procedure can load any text file into any TMemo. In this case, 'Memo1' is just a label for the procedure to know what TMemo to update.

Try adding a button to your application, and in the OnClick handler (just doubleclick the button in designer), add a call to this procedure:
ReadFromFile( Memo1, '/root/test.txt' );

Know when you click the button when running your app, the recipe will be executed, with ingredients Memo1 (the TMemo on your form) and the filename '/root/test.txt';

To play a bit more, try adding a second TMemo (Memo2) and button (Button2) to your app, and in the OnClick of Button2 add:
ReadFromFile( Memo2, '/root/test.txt' );

This will read the same text file into Memo2, using the exact same procedure (recipe)!

Good luck.
0
 

Author Comment

by:itektas
ID: 9677695
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,
0
 

Expert Comment

by:computerprogramer
ID: 9683290
This was my code a whle ago ...

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

618 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