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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

READING A DELPHI TYPE FILE

Status
Not open for further replies.

hbez

Instructor
Mar 25, 2003
49
ZA
I have a form with a ListBox and 2 buttons. I declare a type
TName = class
CustName : string;
end;

var
MyFile : file of TName;
cName : TName;

Then use the following to add or display data:

procedure TForm1.btnAddClick(Sender: TObject);
begin
if Length(edtName.Text)<1 then Exit;
AssignFile(MyFile,'dFile.dat');
FileMode := fmOpenWrite;
if FileExists('dFile.dat') then
begin
Reset(MyFile);
Seek(MyFile,FileSize(MyFile));
end
else
Rewrite(MyFile);
cName := TName.Create;
cName.CustName := edtName.Text;
write(MyFile,cName);
CloseFile(MyFile);
end;

procedure TForm1.btnShowClick(Sender: TObject);
begin
lst1.Clear;
if not FileExists('dFile.dat') then Exit;
AssignFile(MyFile,'dFile.dat');
FileMode := fmOpenRead;
Reset(MyFile);
try
while not Eof(MyFile) do
begin
read(MyFile,cName);
lst1.Items.Add(cName.CustName);
end;
finally
CloseFile(MyFile);
end;
end;

This works fine if the file is created from scratch - I can add data and show it in the Listbox without a problem.
HOWEVER, if the file already exists when I start the program I can add data BUT, when I attempt to show the data, I get an error.
Can someone please point out the error in my ways?
Hannes
 
Looks like you're complicating things unnecessarily.

I'd ditch your TName class, and declare MyFile as
Code:
var
  MyFile: TextFile;

And then, instead of seeking to the end of the existing file, just use the Append instead of Rewrite (which opens an existing file and any Writes to it are appended).

Wherever you're using the TName class, just use a String.

That should get you working.
 
Further - in your specific example, you may find it a lot easier in your program to simply use lst1.Items.LoadFromFile('dfile.dat') in your FormCreate event, and lst1.Items.SaveToFile('dfile.dat') in your FormDestroy event.

This will ensure that your listbox remembers it's items after the program is closed.
 
To address your program specifically, you can't have a file composed of a class or an object (the compiler won't complain but you won't get the results you are going after). This represents the chief problem of your program. If you do use a record structure, it has to be of finite size. For example:

Code:
type
  string255 = string[255];
  TFName = record
    CustName:string255;
  end;
var
  MyFile : file of TFName;
  cName : TFName;

The other comment that I can make is that it would be best to just go with the intents of the file open commands you use instead of using FileMode.

If you make the above change illustrated in code along with the corresponding changes in your program it will work properly.


Measurement is not management.
 
I solved the problem by declaring
type
TName = record
CustName : string[20];
end;

I'm not using a text file to 'cloak' the data, for want of a better word. My problem started by not realizing that one cannot simply declare CustName as a string in a type file.

Thanks to everyone for the response.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top