How to put in data - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to put in data

How do i put in data in my members.dat?
I use the sample at http://delphi.about.com/od/fileio/a/fileof_delphi.htm

"[italic]Write to a File
Suppose we have filled an array of Delphi members with their names, e-mails and number of posts and we want to store this information in a file on the disk. The following peace of code will do the work:

var
F : file of TMember;
i : integer;
begin
AssignFile(F,'members.dat') ;
Rewrite(F) ;
try
for j:= 1 to 50 do
Write (F, Members[j]) ;
finally
CloseFile(F) ;
end;
end;[/italic]
"


Comments

  • DWDuckDWDuck Posts: 31Member
    Not really sure what you want cause the same explain everything pretty good.

    The only ammendment I would make to the code is the "Rewrite(F)"

    The reason is Rewrite(F) creates the "members.dat" file on the harddrive, if its already created the data in it will be lost after the next rewrite. The following code will fix that:
    {$I-} {Turns of the Input output error check}
    Reset(F); {Resets the cursor to the first record, if the file does not exists then ioresult's value will be replaced by the error value returned}
    If ioresult <> 0 then {checks whether there was an error with the file reset}
    Rewrtite(F); {creates the file if it doesn't exists}
    {$I+} {Turns back on the input output error checking}

    Further more if you wanted to know which part of the code you have physically puts the data into file its the "write()" procedure.

    Now the only other thing I can now think of might be an issue is to get values to the variables of the record.

    Since the example is using a record array to give a value to the first member's name the code will look like this.

    members[1].name := 'Darkwing Duck';

    Carry on like this filling in all the variables in the record for every array you need...

    Let me know if this little article covered you problem or not.
    Darkwing Duck aka DWduck signing off :)
Sign In or Register to comment.