In my program I did the following:

begin
if not OpenDialog1.Execute then Exit;
Memo1.Clear;
AssignFile(MyFile,OpenDialog1.FileName);
Reset(MyFile);

while not EOLN(MyFile) do
begin

Read(MyFile,Text1);
Memo1.Lines.Add(Text1);
end;
closeFile(MyFile);
end;

Can any one help me to write each word from the text file word by word in the memo.

Regards,
NOna

Recommended Answers

All 5 Replies

OK, and in what way did you not achieve your aim, and what did you try to do to fix it.

Please also use code tags to make reading your code more readable.

why not try the following:

I've tried to make it as easy to follow as possible and it could be optimized (I'll let you work out how to do that if you want to) but the code works and I hope it helps.

procedure TfrmMain.cmdOpenClick(Sender: TObject);

var
   fle   : TextFile;
   iPos  : Integer;
   sPath : String;
   sLine : String;
   sWord : String;

begin
  if not OpenDialog1.Execute then Exit;
  Memo1.Lines.Clear;
  sPath := OpenDialog1.FileName;
  AssignFile(fle,sPath);
  Reset(fle);
  while not eof(fle) do
  begin
    Readln(fle,sLine);
    sLine := Trim(sLine);
    iPos  := Pos(' ',sLine);
    while iPos > 0 do
    begin
      sWord := Trim(Copy(sLine,1,iPos));
      Memo1.Lines.Add(sWord);
      Delete(sLine,1,iPos);
      sLine := Trim(sLine);
      iPos  := Pos(' ',sLine);
    end;
    if Length(sLine) > 0 then
    begin
      Memo1.Lines.Add(sLine);
    end;
  end;
end;

Try running that on say windows.pas, how long does that take? answer: Ages.

Dont use delete.. no point, its incredibly bad on memory as each time you are allocating and unallocating and moving it around.

OK, it does take a while but enclosing the main loop within a Memo1.BeginUpdate and Memo1.EndUpdate speeds it up, I'll admit, it's not the quickest way to do it but I'd just joined and thought I would provide a quick and simple (albeit not fast!) way to solve the problem.

delete is a bad bad idea if you can avoid it. Which you can. Have a go at doing it without the delete, its a good fun thing to work on as it helps you think about speed issues for other matters another day.

I set it as a challenge for a bunch before, to take windows.pas and count the number of words eg no numbers, punctuation etc. it was a good challenge, we had some great answers and everyone learnt.. its good.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.