954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read text file word by word

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

bulkhin
Newbie Poster
7 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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;
andyrgray1970
Newbie Poster
2 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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.

andyrgray1970
Newbie Poster
2 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You