saving records - 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.

saving records

jogujogu Posts: 10Member
i have a program which deals with RECORDS file structure. Now i want to save the entries i obtained after running the program on a text file and be able to retrieve this data when i run the program again so that i dont need to retype and retype data everytime i run the program. Thanks in advance guys!

Comments

  • sweeneysweeney Posts: 117Member
    [b][red]This message was edited by sweeney at 2003-3-14 9:23:15[/red][/b][hr]
    : i have a program which deals with RECORDS file structure. Now i want to save the entries i obtained after running the program on a text file and be able to retrieve this data when i run the program again so that i dont need to retype and retype data everytime i run the program. Thanks in advance guys!
    :
    I did a reply but my code was all crap lol here is a better explamation!

    ok

    [code]
    var
    F : text;
    mystring : string;

    begin
    write(' Please enter a string: ');
    readln(mystring);
    assign(f, 'C: est.txt');
    rewrite(f);
    writeln(f, mystring);
    close(f)
    end.
    [/code]
    this will rewite your answer every time do this to make the file then change the rewrite() to reset() and that will allow you to do it over and over!
    hope this helps! am just trying to find a way to read it now!


  • Phat NatPhat Nat Posts: 757Member
    : i have a program which deals with RECORDS file structure. Now i want to save the entries i obtained after running the program on a text file and be able to retrieve this data when i run the program again so that i dont need to retype and retype data everytime i run the program. Thanks in advance guys!
    :

    To save and Read a record, use the following:

    [code]
    TYPE
    Person = record
    Name : String;
    Age : Byte;
    Address : String;
    End;

    VAR
    Bob : Person;

    PROCEDURE Save(P : Person);
    VAR
    F : File;
    Begin
    Assign(F,'Save.Dat'); Rewrite(F,1);
    BlockWrite(F, P, SizeOf(P));
    Close(F);
    End;

    FUNCTION Load(VAR P : Person) : Boolean;
    VAR
    NumRead : Word;
    Temp : Person;
    Begin
    {$I-}
    Assign(F,'Save.Dat'); Reset(F,1);
    {$I+}
    If IOResult <> 0 Then
    Begin
    WriteLn('File Not Found!);
    Load := False;
    Exit;
    End;
    BlockRead(F, Temp, SizeOf(Temp),NumRead);
    Close(F);
    If NumRead <> SizeOf(Temp) Then
    Begin
    WriteLn('File Wrong Size!);
    Load := False;
    Exit;
    End;
    P := Temp;
    Load := True;
    End;

    Begin
    If Load(Bob) = False Then
    Begin
    Bob.Name := 'Bob';
    Bob.Age := 33;
    Bob.Address := '1234 ABC Drive';
    Save(Bob);
    WriteLn('Your info has been saved!);
    End
    ELSE
    Begin
    WriteLn('Your info has been loaded!);
    WriteLn(' Your name is :',Bob.Name);
    WriteLn(' Your age is :',Bob.Age);
    WriteLn(' Your live at :',Bob.Address);
    End;
    End.
    [/code]

    Anyways this is just an example. The Record passing to the procedures may not work (I didn't test it), but the Loading and Saving should be right.

    hope this helps,
    Phat Nat

  • jogujogu Posts: 10Member
    : To save and Read a record, use the following:
    :
    : [code]
    : TYPE
    : Person = record
    : Name : String;
    : Age : Byte;
    : Address : String;
    : End;
    :
    : VAR
    : Bob : Person;
    :
    : PROCEDURE Save(P : Person);
    : VAR
    : F : File;
    : Begin
    : Assign(F,'Save.Dat'); Rewrite(F,1);
    : BlockWrite(F, P, SizeOf(P));
    : Close(F);
    : End;
    :
    : FUNCTION Load(VAR P : Person) : Boolean;
    : VAR
    : NumRead : Word;
    : Temp : Person;
    : Begin
    : {$I-}
    : Assign(F,'Save.Dat'); Reset(F,1);
    : {$I+}
    : If IOResult <> 0 Then
    : Begin
    : WriteLn('File Not Found!);
    : Load := False;
    : Exit;
    : End;
    : BlockRead(F, Temp, SizeOf(Temp),NumRead);
    : Close(F);
    : If NumRead <> SizeOf(Temp) Then
    : Begin
    : WriteLn('File Wrong Size!);
    : Load := False;
    : Exit;
    : End;
    : P := Temp;
    : Load := True;
    : End;
    :
    : Begin
    : If Load(Bob) = False Then
    : Begin
    : Bob.Name := 'Bob';
    : Bob.Age := 33;
    : Bob.Address := '1234 ABC Drive';
    : Save(Bob);
    : WriteLn('Your info has been saved!);
    : End
    : ELSE
    : Begin
    : WriteLn('Your info has been loaded!);
    : WriteLn(' Your name is :',Bob.Name);
    : WriteLn(' Your age is :',Bob.Age);
    : WriteLn(' Your live at :',Bob.Address);
    : End;
    : End.
    : [/code]

    How will i modify the program because part of my code goes like this..

    [code]
    CONST

    max= 50;

    TYPE
    : Person = record
    : Name : String;
    : Age : Byte;
    : Address : String;
    : End;
    : People = array(1..max) of person;
    : VAR
    friends : people;


    [/code]

    my program should be able to save RECORD of friends everytime i needed to (this might be before quitting the program or when the user calls the save function) and retrieve the saved file (i need to save it on a *.txt format) everytime i start the program. You're help Phat Nat is very well appreciated. Thanks.
  • Phat NatPhat Nat Posts: 757Member
    : : To save and Read a record, use the following:
    : :
    : : [code]
    : : TYPE
    : : Person = record
    : : Name : String;
    : : Age : Byte;
    : : Address : String;
    : : End;
    : :
    : : VAR
    : : Bob : Person;
    : :
    : : PROCEDURE Save(P : Person);
    : : VAR
    : : F : File;
    : : Begin
    : : Assign(F,'Save.Dat'); Rewrite(F,1);
    : : BlockWrite(F, P, SizeOf(P));
    : : Close(F);
    : : End;
    : :
    : : FUNCTION Load(VAR P : Person) : Boolean;
    : : VAR
    : : NumRead : Word;
    : : Temp : Person;
    : : Begin
    : : {$I-}
    : : Assign(F,'Save.Dat'); Reset(F,1);
    : : {$I+}
    : : If IOResult <> 0 Then
    : : Begin
    : : WriteLn('File Not Found!);
    : : Load := False;
    : : Exit;
    : : End;
    : : BlockRead(F, Temp, SizeOf(Temp),NumRead);
    : : Close(F);
    : : If NumRead <> SizeOf(Temp) Then
    : : Begin
    : : WriteLn('File Wrong Size!);
    : : Load := False;
    : : Exit;
    : : End;
    : : P := Temp;
    : : Load := True;
    : : End;
    : :
    : : Begin
    : : If Load(Bob) = False Then
    : : Begin
    : : Bob.Name := 'Bob';
    : : Bob.Age := 33;
    : : Bob.Address := '1234 ABC Drive';
    : : Save(Bob);
    : : WriteLn('Your info has been saved!);
    : : End
    : : ELSE
    : : Begin
    : : WriteLn('Your info has been loaded!);
    : : WriteLn(' Your name is :',Bob.Name);
    : : WriteLn(' Your age is :',Bob.Age);
    : : WriteLn(' Your live at :',Bob.Address);
    : : End;
    : : End.
    : : [/code]
    :
    : How will i modify the program because part of my code goes like this..
    :
    : [code]
    : CONST
    :
    : max= 50;
    :
    : TYPE
    : : Person = record
    : : Name : String;
    : : Age : Byte;
    : : Address : String;
    : : End;
    : : People = array(1..max) of person;
    : : VAR
    : friends : people;
    :
    :
    : [/code]
    :
    : my program should be able to save RECORD of friends everytime i needed to (this might be before quitting the program or when the user calls the save function) and retrieve the saved file (i need to save it on a *.txt format) everytime i start the program. You're help Phat Nat is very well appreciated. Thanks.
    :

    Thank you, I try to help whenever I can.
    Is there any particular reason why you need to save it as a .txt file?
    Do you need to access it in an outside database because the file will be smaller when it is saved as data (in this case between 100-200 bytes) and the code for doing it is smaller (about 3-4 lines). If you can use the Data format, I would suggest it (as done above) otherwise this would be how you use it as text:

    [code]
    CONST
    max= 50;

    TYPE
    Person = record
    Name : String;
    Age : Byte;
    Address : String;
    End;
    People = array(1..max) of person;
    VAR
    friends : people;

    PROCEDURE Save(Info : People);
    Begin
    Assign(T, 'People.Txt'); Rewrite(T);
    For X := 1 to max Do
    Begin
    WriteLn(T, Info[X].Name);
    WriteLn(T, Info[X].Age);
    WriteLn(T, Info[X].Address);
    End;
    Close(T);
    End;

    PROCEDURE Load(Info : People);
    Begin
    Assign(T, 'People.Txt');
    {$I-} Reset(T); {$I+}
    If IOResult <> 0 then
    Begin
    WriteLn('File not found!');
    Exit;
    End;
    For X := 1 to max Do
    Begin
    ReadLn(Info[X].Name);
    ReadLn(Info[X].Age);
    ReadLn(Info[X].Address);
    If EOF(T) Then Break; { In case the file isn't long enough }
    End;
    Close(F);
    End;

    Begin {Used like this:}
    Load(People);
    Save(People);
    End.
    [/code]

    This is how it would be done. The only problam with doing it as text (as you may be able to see) is that if you have 100 different variables in your record, it would take a LONG time to write out all the WriteLn(...); lines. Anyways, hope this helps.

    Phat Nat
  • jogujogu Posts: 10Member
    I'll try that Phat Nat. Thanks a lot again. =)
Sign In or Register to comment.