Jump to content
stephanos

Read in multiple lines and display them in a TMemo object

Recommended Posts

Dear All

 

25 years ago I wrote a small database in Pascal for my A Level. 1 day ago I installed Lazarus as I want to code a Delphi Object Pascal project to write .m3u (Playlist) as plain text files, so as to save me manually writing playlist for my mp3 player. Each line is going to be an absolute path to a file that is on the mp3 player. Something like this:

\Music\Akmed's Camel\Akmed's Camel\The Boston Flyer.mp3

Granted this is nearly a pointless exercise. I chose it as a starting piece to learn Delphi object programming.

 

I have a simple text file with two lines of text and a third line that is blank. The content was generated by two WriteLn() statements and then the file was closed.

Here is the content of the experimental m3u file so far:

Hello World

more text

blank line

 

Next I want to press a button that opens the file and reads in each line and either per line writes the line to a Tmemo object, Memo1.Text := ???? or reads in each line to a string and then pastes the string into Memo1.Text := ????. I want the content to be reproduced over 2 lines. In theory I will then remove items I do not want and paste in items I do want.

 

Here is my attempt so far:

procedure TForm1.Button4Click(Sender: TObject);

begin

AssignFile(myFile, 'Test.txt');

Reset(myFile);

while not Eof(myFile) do

begin

ReadLn(myFile, lineoftext);

Memo1.Text := lineoftext;

//finaltext := finaltext;

end;

CloseFile(myFile); // Close the file

end;

 

Here is the content of Memo1.Text after clicking the button:

more text

 

I have lost the first line and nearly the will to live. I understand that readLn excludes a the newline instruction. That is half my problem. The other half is how to handle the content of each iteration of readLn. The remaining half is how to send the content Memo1. A problem of three halves you might say.

 

Windows 10, 64 bit

Free Pascal Lazarus Project, version2.0.6

Share this post


Link to post
begin
  Memo1.Lines.LoadFromFile('Test.txt');  // loads the entire file as is into the memo

It is a bit unclear what you want to do per line, and if it can be automated or not?

 

Do you have a collection of files that you want to merge into the Memo ?

var
  Filename: string;
  FileContent: TStringList;
begin
  FileContent := TStringList.Create;
  try
    for FileName in ListOfFileNames
    do begin
      FileContent.LoadFromFile(FileName);
      // Process it in FileContent here
      Memo1.Lines.AddStrings(FileContent);
    end;
  finally
    FileContent.Free;
  end;
end;

 

Share this post


Link to post
22 hours ago, stephanos said:

 


Memo1.Text := lineoftext;

 

Setting the Text property replaces the ENTIRE contents of the Memo.  If you want to APPEND a line to the EXISTING text, use the Lines.Add() method instead, eg:

Memo1.Lines.Add(lineoftext);

In which case, your entire loop can be replaced with a single LoadFromFile() instruction, eg:

procedure TForm1.Button4Click(Sender: TObject);
begin
  Memo1.Lines.LoadFromFile('Test.txt');
end;

Or, if you want to append the entire file to the end of the EXISTING text, do what Lars Fosdal showed you - load the file into a TStringList first, then you can AddStrings() that into the TMemo.

 

If you really want to manually loop through a text file line-by-line, consider using the TStreamReader class instead of old-style Pascal file I/O.  TStreamReader has a ReadLine() method (and in Delphi, it supports Unicode text encodings), eg:

procedure TForm1.Button4Click(Sender: TObject);
var
  FS: TFileStream;
  Reader: TStreamReader;
begin
  FS := TFileStream.Create('Test.txt', fmOpenRead or fmShareDenyWrite);
  try
    Reader := TStreamReader.Create(FS);
    try
      while not Reader.Eof do
      begin
        lineoftext := Reader.ReadLine(myFile);
        Memo1.Lines.Add(lineoftext);
      end;
    finally
      Reader.Free;
    end;
  finally
    FS.Free;
  end;
end;

 

Share this post


Link to post

Dear All

Thanks. Resolved with this:

while not Eof(myFile) do // read the file contents line by line

begin

ReadLn(myFile, lineoftext); // read in a line into a string

filecontents.Add(lineoftext); // add occurrence to filecontents a TStringList

end; // when all the lines are read from and into filecontents

Memo1.Lines := filecontents; // write filecontents to the Memo window

 

I will experiment with LoadFromFile as this appears to be more direct and look at INI as well.

Share this post


Link to post

Dear All

 

I have now implemented

Memo1.Lines.LoadFromFile('Test.txt');

This one line replaces 8 other lines of code.

 

Thank you again

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×