Howdy, Stranger!

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

Categories

save file??

zerarzerar Member Posts: 8
Need to know how you save a you work in a file. I made a little bank account program and need to save the information in a file. Please help me!

Comments

  • netgertnetgert Member Posts: 331
    : Need to know how you save a you work in a file. I made a little bank account program and need to save the information in a file. Please help me!
    :

    First declare a type [b]TAccount[/b] representing the account. Then declare a variable [b]AccountFile[/b] of type [italic]file of [/italic][b]TAccount[/b]. After that declare another variable [b]Account[/b] of type [b]TAccount[/b]. Now do something like this:

    [code]
    {...}
    AssignFile([b]AccountFile[/b], [b]'Account.dat'[/b]); //Assign a name to the file variable
    Rewrite([b]AccountFile[/b]); //Clear the file and open it for output
    Write([b]AccountFile[/b], [b]Account[/b]); //Write your account variable
    CloseFile([b]AccountFile[/b]); //Close the file
    {...}
    [/code]

    You may change the words in bold to the names declared in your project/unit.

    [italic][red]N[/red][blue]et[/blue][red]G[/red][blue]ert[/blue][/italic]

  • jananijanani Member Posts: 16
    As an example for a friend who had a similar problem as you I wrote a mini program which literally only saves data input into a string grid to a file.


    The code was:
    {Under the private/public declarations}
    Tsclub = record
    sname : string[20];
    age : string[20];
    animal : string [20];
    colour : string[20];
    end;
    {these are all the fields in your record}

    var
    Form1: TForm1;
    sclub : array [1..7] of Tsclub;
    sclubfile : file of Tsclub;

    -------------------------------------------
    {the actual procedure is}
    var i,j : integer;
    begin
    for i:=1 to 7 do
    begin
    {Transfers data from stringgrid to the record}
    sclub[i].sname:=grid.cells[0,i];
    sclub[i].age:=grid.cells[1,i];
    sclub[i].animal:=grid.cells[2,i];
    sclub[i].colour:=grid.cells[3,i];

    assignfile(sclubfile,'Z: estingsc7.dat');
    rewrite(sclubfile);
    for j:=1 to 7 do
    begin
    write(sclubfile,sclub[j]);
    end;
    closefile(sclubfile);
    end;

    N.B.sc7.dat is the name of the file it will save to - it auto creates for you
    ------------------------------------


    To read the information back into the array (e.g. once you've closed the program down and then run it again you need to re-load the info to be able to do anything to it:)

    procedure TForm1.FormCreate(Sender: TObject);
    var i, count : integer;
    begin

    assignfile(sclubfile,'Z: estingsc7.dat');
    reset(sclubfile);
    count:=0;
    repeat
    count:=count+1;
    read(sclubfile,sclub[count]);
    until eof(sclubfile);
    closefile(sclubfile);

    for i:= 1 to count do
    begin
    grid.cells[0,i]:=sclub[i].sname;
    grid.cells[1,i]:=sclub[i].age;
    grid.cells[2,i]:=sclub[i].animal;
    grid.cells[3,i]:=sclub[i].colour;
    end;
    end;

    -----------------------------------------------------

    And that's it.

Sign In or Register to comment.