Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

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

Report
how to update record in array in file? please check my existed code Posted by rockz on 22 Feb 2012 at 12:46 PM
hi, im currently making a program where it records student's points, and teachers can update the points if they want. so now im on the procedure of update upon the existed points, but i've got this problem where after i updated one student's points, if i check back ,all my other student's records are gone! only left the one who updated correctly...i think i know what's the problem, because i used rewrite in the end of the procedure, but i just cant find another way of where i can successfully update one's data but keep others as well, please someone help!

it also be great if you can give me extra tips of how to improve my code, thanks!

Procedure AddPoints;

var newPoints, Sum, i, count: Integer;
TempStudent: TStudent;

begin
Clrscr;
Reset (StudentFile);

{Put all the persons in an array.}
NumStudents:=0;
while not eof(Studentfile) do
begin
inc(NumStudents); // make NumStudents =1;
read(Studentfile,Students[NumStudents]) // so it reads every stuents in 1 array;
end;

write('Enter the Student ID: ');
readln(id);
i:=1;
while (i<=NumStudents) and (Students[i].id<>id) do
inc(i);
{Display that person's detail from the array.}

if id = Students[i].id then
Writeln (' Please enter the new points');
Readln (newPoints);

Sum:= Students[i].points + newPoints;
tempStudent.points := Sum;

Students[i].points := TempStudent.points;

assign(StudentFile,'Student.dat');
rewrite(StudentFile);

Write (StudentFile, Students[i]);
Closefile(StudentFile); 
Report
Re: how to update record in array in file? please check my existed Posted by Crono84 on 22 Feb 2012 at 4:31 PM
Well you first read the file recursive record by record witch tells me that the file is a file of type TStudent
and on the end You try to put the whole array by one time to it
try this
assign(StudentFile,'Student.dat');
rewrite(StudentFile);

i:=0;
repeat 
Write (StudentFile, Students[i]);
inc(i);
until i>NumStudents;
Closefile(StudentFile); 

or You can try this two
Write (StudentFile, Students);
{without the [i]}

and if needed the count of TStudent records to write
Write (StudentFile, Students,NumStudents);



Report
Re: how to update record in array in file? Posted by Actor21 on 28 Feb 2012 at 12:48 PM
Procedure AddPoints;

type
   TStudent = record
      id       : string ;
      points   : integer
   end ;

var
   id          : string ;
   newPoints   : Integer ;
   TempStudent : TStudent ;
   StudentFile : file of TStudent ;
   i, n        : longint ;

begin
   Clrscr ;

   write('Enter the Student ID: ') ;
   readln(id) ;

   assign (StudentFile, 'Student.dat') ;
   Reset (StudentFile) ;
      n := filesize(StudentFile) - 1 ; { index of last record in file }
      for i := 0 to n do begin
         seek(StudentFile, i) ;
         read(StudentFile, Tempstudent) ;
         if TempStudent.id = id then begin
            Writeln (' Please enter the new points') ;
            Readln (newPoints) ;
            with tempstudent do
               points := points + newPoints ;
            seek(StudentFile, i) ;
            write(StudentFile, TempStudent) ;
            close(StudentFile) ;
            exit
         end
      end ;
   Close (StudentFile) ;
   {
      if we reach this point then the record was not found
   }
   writeln ('Student id ', id, ' not found.') ;
   writeln ('Hit ENTER to continue') ;
   readln
end ;




 

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.