Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate TListBox from file

Status
Not open for further replies.

derwoood

Programmer
Oct 3, 2007
2
US
I have a file containing one 5 letter word per line and x number of words total. How can I open the file and display all the words in a ListBox? I have had limited success with this code but it only adds the first line and if run more than once it returns an error;

var
myFile: TextFile;
S: string;

procedure TForm1.edtFirstChange(Sender: TObject);
begin

AssignFile(myFile, edtFirst.Text + '.txt');
Reset(myFile);
ReadLn(myFile, S);
ListBox1.Items.Text := S;
CloseFile(myFile);

end;


I have also tried the following to try to read all the way down to the eof but it doesn't work either

procedure TForm1.edtFirstChange(Sender: TObject);
begin

AssignFile(myFile, edtFirst.Text + '.txt');
Reset(myFile);
while not Eof(myFile) do
begin
ReadLn(myFile, S);
ListBox1.Items.Text := S;
CloseFile(myFile);
end;
end;

I have also tried ListBox1.Items.Add := S; instead of .Text but that returns a complier error. What am I doing wrong?

Thx
D
 
heres and easyer way
Code:
ListBox1.Items.LoadFromFile('c:\myitems.txt');

Aaron
 
also the reason your second example didnt work is because you are closing the file in the read line loop.

something like this might work.

Code:
procedure TForm1.edtFirstChange(Sender: TObject);
var
  myFile: TextFile;
  S: string;
begin
 AssignFile(myFile, edtFirst.Text + '.txt');
 try
  Reset(myFile);
  repeat
    ReadLn(myFile, S);
    ListBox1.Items.add(S);
  until EOF(myFile);
 finally
  CloseFile(myFile);
end;

Aaron
 
Aaron, a minor point in your example code with the try..finally block - it's good practice to put the Reset before the try, and not within the try..finally block. That way if the file cannot be opened, Reset will throw an exception, the file will still be in a closed state, and CloseFile will not be called (which could presumably throw another exception).
 
Griffyn;

Isn't that the PURPOSE of putting the reset within the TRY?

I typically don't use TRY on file access like this. I normally resort to
Code:
{$i-}
reset(myFile);
{$i+}
ResetResult:=IOResult;

Then decide what to do on ResetResult. During the read loop, sure, throw that in the TRY.

Although I haven't tried, the other thing that can be done is instead of TRY/FINALLY is TRY/EXCEPT. Something like this may work (UNTESTED CODE)

Code:
try
  reset(myFile);
  try
    while not eof(myFile) do begin
      readln(myFile,InputString);
      Combo.items.add(InputString);
    end;
  except
    closefile(MyFile);
  end;
finally
  application.messagebox('Process completed successfully','Congrats',0);
end;

I would also check for the EOF of what we're reading in case its a 0-byte file. If you start digging into the read and it just so happens that it is a 0-byter, then you will raise an exception. This way nothing gets added to the combo box and there is no exception to deal with.

-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=
NEVER send the boss to do a techs job
 
Wow, thanks for all the examples everyone. That's just what I needed.
 
Bug in my code. In the final FINALLY you need to add the closefile procedure. Before or after the messagebox, doesn't matter.

-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=
NEVER send the boss to do a techs job
 
Hi CamaroLT,

You would put the Reset within a try..except block, but just before a try..finally block.

Also, best practice would suggest putting the CloseFile call before the messagebox call - that way you have the file closed while the system waits for the user. There's no sense in it remaining open.
 
I see your point on the try/finally vs try/except. try/finally would still throw exceptions if the file isn't accessable, isn't open, etc.

Good call on the position of the close file as well. Not written in stone, but as a general rule of thumb when designing software is use the resources as quickly as possible. There are exceptions to the rule of course.

-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=
NEVER send the boss to do a techs job
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top