how to update record in array in file? please check my existed code - Programmers Heaven

Howdy, Stranger!

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


how to update record in array in file? please check my existed code

rockzrockz Posts: 1Member
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!

[code]Procedure AddPoints;

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

Reset (StudentFile);

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

write('Enter the Student ID: ');
while (i<=NumStudents) and (Students[i].id<>id) do
{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;


Write (StudentFile, Students[i]);
Closefile(StudentFile); [/code]


  • Crono84Crono84 Posts: 6Member
    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

    repeat [/b]
    Write (StudentFile, Students[i]);
    until i>NumStudents;[/b]
    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,[b]NumStudents[/b]);

  • Actor21Actor21 Posts: 35Member
    Procedure AddPoints;

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

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

    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 = 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) ;
    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') ;
    end ;
Sign In or Register to comment.