home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / pascal / 7386 < prev    next >
Encoding:
Text File  |  1992-12-11  |  6.5 KB  |  193 lines

  1. Path: sparky!uunet!ornl!utkcs2!darwin.sura.net!zaphod.mps.ohio-state.edu!caen!batcomputer!ghost.dsi.unimi.it!univ-lyon1.fr!cismibm.univ-lyon1.fr!ppollet
  2. From: ppollet@cismibm.univ-lyon1.fr (Patrick POLLET)
  3. Newsgroups: comp.lang.pascal
  4. Subject: Re: TRescourceFile
  5. Date: Fri, 11 Dec 1992 18:54:28 GMT
  6. Organization: INSA  CENTRE INFORMATIQUE DU 1er CYCLE
  7. Lines: 181
  8. Message-ID: <ppollet.66.724100068@cismibm.univ-lyon1.fr>
  9. References: <1992Dec11.144151.3239@jupiter.sun.csd.unb.ca>
  10. NNTP-Posting-Host: 134.214.232.25
  11.  
  12. In article <1992Dec11.144151.3239@jupiter.sun.csd.unb.ca> tcsmith@mta.ca (Tim Smith) writes:
  13. >From: tcsmith@mta.ca (Tim Smith)
  14. >Subject: TRescourceFile
  15. >Date: Fri, 11 Dec 1992 14:41:51 GMT
  16.  
  17. >  I'm having a problem with TResourceFile in TP6 that I hope
  18. >someone can suggest a solution to. I Know; " Ugh! Another
  19. >TVision problem" but I spent alot of time sifting through the
  20. >TVsion manual to get this far and I hate to see it all go to
  21. >waste.
  22.  
  23.  
  24.    I read your listing that looked OK to me execpt at the last lines.
  25.    You Disposed the Stream and the Disposed the ResourceFile that 
  26.    used that stream. thus disposing twice the Stream... But it is not you 
  27.    problem since your program BOMBED Before that line...
  28.  
  29. My guess is that your problem is not here but in the Unit that define the 
  30. student objects. since i had no information I made up one out of the
  31. data you gave us. According to method tStnDt.DiplayIn or DisplayOut, it 
  32. seems that 
  33.  Name is a pointer to a record that holds two strings (and not  
  34. to pStrings)
  35.  Id is a pString...
  36.  I am afraid that your problem come from somewhere in the Init, load or may 
  37. store of these objects.
  38.   Since I had no way to check i wrote something that works ... With comments
  39. to help you improve it ...
  40.  
  41. uses Objects;const  StdntFileName:FnameStr='Stdnt.dat';
  42.  
  43. (* simulation of your student objet NOT provided *)
  44. Type
  45.       Str20=String[20];
  46.       pData=^tData;
  47.       tData =record
  48.               last :Str20;
  49.               first:Str20;
  50.              end;
  51.  
  52.       pStudentObj=^tStudentObj;
  53.       tStudentObj=object(tObject)
  54.          name   :pData;
  55.          Id     :pString;
  56.          Constructor Init ;
  57.          Destructor Done; VIRTUAL;
  58.          Constructor Load (var S:tStream);
  59.          Procedure Store( var S:tstream);
  60.        end;
  61.  
  62. Constructor tStudentObj.Init;
  63. begin
  64.   tObject.Init;   (* likely not needed sinc all fields are initialiezd below ? *)
  65.   New(name);      (* allocate a Last-First name record DID YOU DO IT ?*)
  66.   Name^.last:='';
  67.   Name^.First:='';
  68.   Id:=Newstr('         ');  
  69.              (* allocate enough space to hold the data !!! *)
  70.              (* quite unsafe if you use a readln later on, but that what
  71.                 you MUST DO if you want that Id is initialized !!!!
  72.                 If You say Id=Newstr('') ==> Id ends up = NIL  
  73.                 so a Redaln(Id^) will be terrible !!!!   
  74. end;
  75.  
  76. Destructor tStudentObj.done;
  77. begin
  78.  If name <>Nil then dispose(Name);  
  79.  if Id <> Nil then DisposeStr(Id)
  80. end;
  81.  
  82. Constructor tStudentObj.Load(var S:tstream);
  83. var AuxStr:pString;
  84. begin
  85.   New(name);          (* allocate a Last-First name record*)
  86.   AuxStr:=S.ReadStr;  (* since the field in Name are static *)
  87.   Name^.last:=AuxStr^;(* you must read the strings using ReadStr *)
  88.   DisposeStr(Auxstr); (* to a auxillary pString, copy the string to*)
  89.   AuxStr:=S.ReadStr;   (* the field and dispose the AuxString *)
  90.   Name^.first:=AuxStr^; (* you would be better off if Last and First *)
  91.   DisposeStr(Auxstr);   (* were pString !!! *)
  92.   Id:=S.readStr;
  93. end;
  94.  
  95. Procedure tStudentObj.Store(var S:tstream);
  96. var AuxStr:pString;
  97. begin
  98.   {$V-}             
  99.   S.WriteStr(@Name^.last);  (* WriteStr wants a pointer so the @ is needed*)
  100.   S.writeStr(@Name^.first);
  101.   S.writestr(Id);
  102. end;
  103.  
  104. type
  105.   PStdntStream=^TStdntStream;
  106.   { I CHANGED FROM TBUFSTEAM TO TDOSSTREAM SINCE IN MY VERSION OF TV (6.0)
  107.     TBUFSTREAM IS CAPRICIOUS . Solved in 6.01 DON'T KNOW ABOUT YOURS ???? *)
  108.  
  109.   TStdntStream= Object(TDosStream)
  110.                   { Overide the old 'Lame' error procedure}
  111.                   procedure Error(Code, Info : Integer);virtual;
  112.                 end;
  113.   PStdnt = ^TStdnt;
  114.   TStdnt = Object(TStudentObj)
  115.              procedure DisplayOut;virtual;
  116.              procedure DisplayIn;virtual;
  117.            end;
  118.  
  119. (*************** YOUR CODE STARTS HERE *******************************)
  120. const
  121.     RStdnt: TStreamRec = (
  122.          ObjType : 13000;
  123.          VmtLink : Ofs(TypeOf(TStdnt)^);
  124.          Load    : @TStdnt.Load ;
  125.          Store   : @TStdnt.Store);
  126. var
  127.   StdntRez : TResourceFile;
  128.   MyStrm   : PStdntStream;
  129.   Stdnt    : PStdnt;
  130. procedure TStdntStream.Error(Code, Info : Integer);
  131. begin
  132.   WriteLn('Stream error: ', Code, ' (',info,') ');
  133.   halt(1);
  134. end;
  135. procedure TStdnt.DisplayOut;
  136.   begin
  137.     writeln('Last name...',Name^.Last);  
  138.     writeln('ID #',Id^);
  139.   end;
  140.  
  141. procedure TStdnt.DisplayIn;
  142.   begin
  143.     write('Input Last Name...');
  144.     Readln(Name^.Last);
  145.     write('Input ID (range 10000, 65535)');
  146.     readln(Id^); (* UNSAFE WHAT YOU SHOULD DO IS:
  147.                       Readln(S); Id:=NewStr(S); and then cancel 
  148.                       initialization of Id I put in the Init Constructor*)
  149.   end;
  150.  
  151. begin
  152.   {** Create and register student **}
  153.   RegisterType(RStdnt);
  154.   Stdnt:=New(PStdnt, Init);
  155. {** Input information **}
  156.   Stdnt^.DisplayIN;
  157.   {** Initialize Resource file **}
  158.   MyStrm:=New(PStdntStream, Init(StdntFileName,stcreate));
  159.   StdntRez.Init(MyStrm);
  160.   {** Save and dispose of Student **}
  161.   StdntRez.Put(Stdnt,'Student1');
  162.   Dispose(Stdnt,Done);
  163.  
  164.   StdntRez.Done; 
  165.  
  166. { closing and Reopening added for checking the File contents
  167.   post mortem using DEBUG }
  168.  
  169.   MyStrm:=New(PStdntStream, Init(StdntFileName,stopen));
  170.   StdntRez.Init(MyStrm);
  171.   {** Load and display student **}
  172.   Stdnt:=PStdnt(StdntRez.Get('Student1'));
  173.    ' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ DO NO CRASH ANYMORE}
  174.   Stdnt^.DisplayOut;
  175. {** Clean up **}
  176.   Dispose(Stdnt,Done);
  177.   (*Dispose(MyStrm,Done);  *)   (*<============= NO NON NEIN NIET !!!! *)
  178.   StdntRez.Done;                (* the stream will be also disposed 
  179. here !!! *)
  180. end.
  181. (*********************************************************************)
  182. ppollet@cismibm.ibm.univ-lyon1.fr (Patrick POLLET)
  183. --------------------------------------------------------
  184. Dr Patrick L.Pollet
  185. Institut National des Sciences Appliquées
  186. Centre Informatique du 1er Cycle  Bat 110
  187. 20 Avenue A.Einstein
  188. 69621 Villeurbanne Cedex France
  189. --------------------------------------------------------
  190. Phone: 72 43 83 80 -   la premiere erreur c'est
  191. Fax  : 72 43 85 33 -   de se lever le matin  ... GASTON
  192. -------------------------------------------------------
  193.