delete procedure - Programmers Heaven

Howdy, Stranger!

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

Categories

delete procedure

sexyderrysexyderry Posts: 31Member
can any one help with a delete procedure please. it is AS coursework and i cant get it to work it compiles but gives a run time error.

begin
clrscr;
assign (pupilfile,'n:pupil.dat');
reset (pupilfile);
writeln ('existing file');
writeln;writeln;writeln;
writeln ('name':15,' Candidate':9,'year':6,'mark':5);
writeln (' ':15,' number ':9,' ':6,' ':5);

writeln;
writeln;
readln;
while not eof (pupilfile) do
begin
read (pupilfile,temp);
writeln (temp.name:15,temp.cand_no:9,temp.year:6,temp.mark:5);
end;

reset (pupilfile);
writeln ('enter candidate number to be deleted');
readln (n);
position:=0;
while not eof (pupilfile) do
begin
read (pupilfile,temp);
position:=(position+1);
if (temp.cand_no=n) then begin
clrscr;
writeln (temp.name:15,temp.cand_no:9,temp.year:6,temp.mark:5);
writeln;writeln;
writeln ('is this the record to be deleted: ');
readln (ans);
if (ans='y') or (ans='Y') then begin
seek (pupilfile,filesize(pupilfile)-1);
truncate (pupilfile);
end;
reset (pupilfile);
clrscr;
writeln ('name':15,'year':10,'mark':10);
writeln;

while not eof (pupilfile) do
begin
read (pupilfile,temp);
writeln (temp.name:15,temp.cand_no:8,temp.year:6,temp.mark:5);
end;
close (pupilfile);

end;
end;

??????????????????????????????? thanks for your help...


Comments

  • roland48roland48 Posts: 23Member
    : can any one help with a delete procedure please. it is AS coursework and i cant get it to work it compiles but gives a run time error.
    :
    : begin
    : clrscr;
    : assign (pupilfile,'n:pupil.dat');
    : reset (pupilfile);
    : writeln ('existing file');
    : writeln;writeln;writeln;
    : writeln ('name':15,' Candidate':9,'year':6,'mark':5);
    : writeln (' ':15,' number ':9,' ':6,' ':5);
    :
    : writeln;
    : writeln;
    : readln;
    : while not eof (pupilfile) do
    : begin
    : read (pupilfile,temp);
    : writeln (temp.name:15,temp.cand_no:9,temp.year:6,temp.mark:5);
    : end;
    :
    : reset (pupilfile);
    : writeln ('enter candidate number to be deleted');
    : readln (n);
    : position:=0;
    : while not eof (pupilfile) do
    : begin
    : read (pupilfile,temp);
    : position:=(position+1);
    : if (temp.cand_no=n) then begin
    : clrscr;
    : writeln (temp.name:15,temp.cand_no:9,temp.year:6,temp.mark:5);
    : writeln;writeln;
    : writeln ('is this the record to be deleted: ');
    : readln (ans);
    : if (ans='y') or (ans='Y') then begin
    : seek (pupilfile,filesize(pupilfile)-1);
    : truncate (pupilfile);
    : end;
    : reset (pupilfile);
    : clrscr;
    : writeln ('name':15,'year':10,'mark':10);
    : writeln;
    :
    : while not eof (pupilfile) do
    : begin
    : read (pupilfile,temp);
    : writeln (temp.name:15,temp.cand_no:8,temp.year:6,temp.mark:5);
    : end;
    : close (pupilfile);
    :
    : end;
    : end;
    :
    : ??????????????????????????????? thanks for your help...
    :
    :
    :

    Take a look in your help-file to get the sense of your runtime-error!
    I gues there is an IO-Error . You are not shure, if the requested file
    exists and you make a reset on an file just opend.
    Use the local compiler-switch {$I-)... Code ...{SI+} to trap your
    error and get the meaning of IOResult!




  • Phat NatPhat Nat Posts: 757Member
    : can any one help with a delete procedure please. it is AS coursework and i cant get it to work it compiles but gives a run time error.
    :
    : begin
    : clrscr;
    : assign (pupilfile,'n:pupil.dat');
    : reset (pupilfile);
    : writeln ('existing file');
    : writeln;writeln;writeln;
    : writeln ('name':15,' Candidate':9,'year':6,'mark':5);
    : writeln (' ':15,' number ':9,' ':6,' ':5);
    :
    : writeln;
    : writeln;
    : readln;
    : while not eof (pupilfile) do
    : begin
    : read (pupilfile,temp);
    : writeln (temp.name:15,temp.cand_no:9,temp.year:6,temp.mark:5);
    : end;
    :
    : reset (pupilfile);
    : writeln ('enter candidate number to be deleted');
    : readln (n);
    : position:=0;
    : while not eof (pupilfile) do
    : begin
    : read (pupilfile,temp);
    : position:=(position+1);
    : if (temp.cand_no=n) then begin
    : clrscr;
    : writeln (temp.name:15,temp.cand_no:9,temp.year:6,temp.mark:5);
    : writeln;writeln;
    : writeln ('is this the record to be deleted: ');
    : readln (ans);
    : if (ans='y') or (ans='Y') then begin
    : seek (pupilfile,filesize(pupilfile)-1);
    : truncate (pupilfile);
    : end;
    : reset (pupilfile);
    : clrscr;
    : writeln ('name':15,'year':10,'mark':10);
    : writeln;
    :
    : while not eof (pupilfile) do
    : begin
    : read (pupilfile,temp);
    : writeln (temp.name:15,temp.cand_no:8,temp.year:6,temp.mark:5);
    : end;
    : close (pupilfile);
    :
    : end;
    : end;
    :
    : ??????????????????????????????? thanks for your help...


    What are all your variables (mainly the TEMP variable) declared as ?

    Phat Nat


  • sexyderrysexyderry Posts: 31Member
    : : can any one help with a delete procedure please. it is AS coursework and i cant get it to work it compiles but gives a run time error.
    : :
    : : begin
    : : clrscr;
    : : assign (pupilfile,'n:pupil.dat');
    : : reset (pupilfile);
    : : writeln ('existing file');
    : : writeln;writeln;writeln;
    : : writeln ('name':15,' Candidate':9,'year':6,'mark':5);
    : : writeln (' ':15,' number ':9,' ':6,' ':5);
    : :
    : : writeln;
    : : writeln;
    : : readln;
    : : while not eof (pupilfile) do
    : : begin
    : : read (pupilfile,temp);
    : : writeln (temp.name:15,temp.cand_no:9,temp.year:6,temp.mark:5);
    : : end;
    : :
    : : reset (pupilfile);
    : : writeln ('enter candidate number to be deleted');
    : : readln (n);
    : : position:=0;
    : : while not eof (pupilfile) do
    : : begin
    : : read (pupilfile,temp);
    : : position:=(position+1);
    : : if (temp.cand_no=n) then begin
    : : clrscr;
    : : writeln (temp.name:15,temp.cand_no:9,temp.year:6,temp.mark:5);
    : : writeln;writeln;
    : : writeln ('is this the record to be deleted: ');
    : : readln (ans);
    : : if (ans='y') or (ans='Y') then begin
    : : seek (pupilfile,filesize(pupilfile)-1);
    : : truncate (pupilfile);
    : : end;
    : : reset (pupilfile);
    : : clrscr;
    : : writeln ('name':15,'year':10,'mark':10);
    : : writeln;
    : :
    : : while not eof (pupilfile) do
    : : begin
    : : read (pupilfile,temp);
    : : writeln (temp.name:15,temp.cand_no:8,temp.year:6,temp.mark:5);
    : : end;
    : : close (pupilfile);
    : :
    : : end;
    : : end;
    : :
    : : ??????????????????????????????? thanks for your help...
    :
    :
    : What are all your variables (mainly the TEMP variable) declared as ?
    :
    : Phat Nat
    :
    :
    :
    they are all in the glabal variable box.

    all the candidates profiles are read into a record, and pupilfile is read into temp.

    type a=record
    name_address:string[15];
    candno_address: integer;
    addressline:string[20];
    town:string[10];
    postcode:string[6];
    end;

    type p=record
    name : string[15];
    cand_no:integer;
    year : integer;
    mark : integer;
    new_grade:char;
    sub_ref_code:string[5];

    var temp : p;
    pupilfile : file of p;
    end;
  • Phat NatPhat Nat Posts: 757Member
    [b][red]This message was edited by Phat Nat at 2003-5-3 17:13:40[/red][/b][hr]
    : can any one help with a delete procedure please. it is AS coursework and i cant get it to work it compiles but gives a run time error.
    :
    [code]
    begin
    clrscr;
    assign (pupilfile,'n:pupil.dat');
    reset (pupilfile);
    writeln ('existing file');
    writeln;writeln;writeln;
    writeln ('name':15,' Candidate':9,'year':6,'mark':5);
    writeln (' ':15,' number ':9,' ':6,' ':5);

    writeln;
    writeln;
    readln;
    while not eof (pupilfile) do
    begin
    read (pupilfile,temp);
    writeln (temp.name:15,temp.cand_no:9,temp.year:6,temp.mark:5);
    end;

    reset (pupilfile);
    writeln ('enter candidate number to be deleted');
    readln (n);
    position:=0;

    [b]while not eof (pupilfile) do[/b]
    begin
    read (pupilfile,temp);
    position:=(position+1);
    if (temp.cand_no=n) then
    begin
    clrscr;
    writeln (temp.name:15,temp.cand_no:9,temp.year:6,temp.mark:5);
    writeln;writeln;
    writeln ('is this the record to be deleted: ');
    readln (ans);
    if (ans='y') or (ans='Y') then
    begin
    seek (pupilfile,filesize(pupilfile)-1);
    truncate (pupilfile);
    end;
    reset (pupilfile);
    clrscr;
    writeln ('name':15,'year':10,'mark':10);
    writeln;

    while not eof (pupilfile) do
    begin
    read (pupilfile,temp);
    writeln (temp.name:15,temp.cand_no:8,temp.year:6,temp.mark:5);
    end;
    [b]close (pupilfile);[/b]
    end;
    end; [b]{ before it can exit here, it checks again up above }[/b]
    [/code]

    Your problem is that you are closing your file and then it checks it again. Since the file is closed, it gives a runtime error when it checks for the end of the file.

    [code]
    {...}
    while not eof (pupilfile) do
    begin
    read (pupilfile,temp);
    writeln (temp.name:15,temp.cand_no:8,temp.year:6,temp.mark:5);
    end;
    end;
    [b]close (pupilfile); { Move the close call here }[/b]
    end;
    [/code]

    This should do it.

    Phat Nat



  • sexyderrysexyderry Posts: 31Member
    : [b][red]This message was edited by Phat Nat at 2003-5-3 17:13:40[/red][/b][hr]
    : : can any one help with a delete procedure please. it is AS coursework and i cant get it to work it compiles but gives a run time error.
    : :
    : [code]
    : begin
    : clrscr;
    : assign (pupilfile,'n:pupil.dat');
    : reset (pupilfile);
    : writeln ('existing file');
    : writeln;writeln;writeln;
    : writeln ('name':15,' Candidate':9,'year':6,'mark':5);
    : writeln (' ':15,' number ':9,' ':6,' ':5);
    :
    : writeln;
    : writeln;
    : readln;
    : while not eof (pupilfile) do
    : begin
    : read (pupilfile,temp);
    : writeln (temp.name:15,temp.cand_no:9,temp.year:6,temp.mark:5);
    : end;
    :
    : reset (pupilfile);
    : writeln ('enter candidate number to be deleted');
    : readln (n);
    : position:=0;
    :
    : [b]while not eof (pupilfile) do[/b]
    : begin
    : read (pupilfile,temp);
    : position:=(position+1);
    : if (temp.cand_no=n) then
    : begin
    : clrscr;
    : writeln (temp.name:15,temp.cand_no:9,temp.year:6,temp.mark:5);
    : writeln;writeln;
    : writeln ('is this the record to be deleted: ');
    : readln (ans);
    : if (ans='y') or (ans='Y') then
    : begin
    : seek (pupilfile,filesize(pupilfile)-1);
    : truncate (pupilfile);
    : end;
    : reset (pupilfile);
    : clrscr;
    : writeln ('name':15,'year':10,'mark':10);
    : writeln;
    :
    : while not eof (pupilfile) do
    : begin
    : read (pupilfile,temp);
    : writeln (temp.name:15,temp.cand_no:8,temp.year:6,temp.mark:5);
    : end;
    : [b]close (pupilfile);[/b]
    : end;
    : end; [b]{ before it can exit here, it checks again up above }[/b]
    : [/code]
    :
    : Your problem is that you are closing your file and then it checks it again. Since the file is closed, it gives a runtime error when it checks for the end of the file.
    :
    : [code]
    : {...}
    : while not eof (pupilfile) do
    : begin
    : read (pupilfile,temp);
    : writeln (temp.name:15,temp.cand_no:8,temp.year:6,temp.mark:5);
    : end;
    : end;
    : [b]close (pupilfile); { Move the close call here }[/b]
    : end;
    : [/code]
    :
    : This should do it.
    :
    : Phat Nat
    :
    :
    :
    :
    I CANNOT BELIEVE I DIDNT SEE THAT!! THANKS ALOT NAT. YOU HERO!! LOL!! CHEERS FOR THAT.
Sign In or Register to comment.