Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
saving records Posted by jogu on 14 Mar 2003 at 5:37 AM
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!
Report
Re: saving records Posted by sweeney on 14 Mar 2003 at 9:22 AM
This message was edited by sweeney at 2003-3-14 9:23:15

: 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

var
 F : text;
 mystring : string;

begin
write(' Please enter a string: ');
readln(mystring);
assign(f, 'C:\test.txt');
rewrite(f);
writeln(f, mystring);
close(f)
end.

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!


Report
Re: saving records Posted by Phat Nat on 14 Mar 2003 at 4:08 PM
: 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:

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.


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

Report
Re: saving records Posted by jogu on 14 Mar 2003 at 10:04 PM
: To save and Read a record, use the following:
:
:
: 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.
: 


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

CONST

max= 50;

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




my program should be able to save <tt>RECORD of friends</tt> everytime i needed to (this might be before quitting the program or when the user calls the <tt>save function</tt>) and retrieve the saved file (i need to save it on a <tt>*.txt</tt> format) everytime i start the program. You're help Phat Nat is very well appreciated. Thanks.
Report
Re: saving records Posted by Phat Nat on 15 Mar 2003 at 10:01 AM
: : To save and Read a record, use the following:
: :
: :
: : 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.
: : 

:
: How will i modify the program because part of my code goes like this..
:
:
: CONST
: 
: max= 50;
: 
: TYPE
: :     Person = record
: :        Name : String;
: :        Age  : Byte;
: :        Address : String;
: :     End;
: :     People = array(1..max) of person;
: : VAR
:       friends : people;
: 
: 
: 

:
: my program should be able to save <tt>RECORD of friends</tt> everytime i needed to (this might be before quitting the program or when the user calls the <tt>save function</tt>) and retrieve the saved file (i need to save it on a <tt>*.txt</tt> 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:

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.


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
Report
Re: saving records Posted by jogu on 15 Mar 2003 at 10:35 PM
I'll try that Phat Nat. Thanks a lot again. =)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.