Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

file of records

faraazfaraaz Posts: 15Member
I am writing a program to add new records, edit existing records and to delete existing records in a file, for example, customer(customer.dat).

i want the program to ask for the name of the customer and it will search for that customer in the file. it will then displays the information about the customer and will ask me to enter the new name, new address, new tel, etc. My program is not searching for the customer. Could anyone help me and give me some codes for editing the file and also for deleting a particular record. Please friends, hope you will reply as soon as possible.
Thanks.

Comments

  • bpajkbpajk Posts: 156Member
    : I am writing a program to add new records, edit existing records and to delete existing records in a file, for example, customer(customer.dat).
    :
    : i want the program to ask for the name of the customer and it will search for that customer in the file. it will then displays the information about the customer and will ask me to enter the new name, new address, new tel, etc. My program is not searching for the customer. Could anyone help me and give me some codes for editing the file and also for deleting a particular record. Please friends, hope you will reply as soon as possible.
    : Thanks.
    :
    :
    Post you're code here and we will help you. We can't know what went wrong in you're search procedure or program if we don't have the code. If you post you're code please use ["CODE"] ["/CODE"] tags. (without the " craracters)
  • li-zili-zi Posts: 23Member
    : I am writing a program to add new records, edit existing records and to delete existing records in a file, for example, customer(customer.dat).
    :
    : i want the program to ask for the name of the customer and it will search for that customer in the file. it will then displays the information about the customer and will ask me to enter the new name, new address, new tel, etc. My program is not searching for the customer. Could anyone help me and give me some codes for editing the file and also for deleting a particular record. Please friends, hope you will reply as soon as possible.
    : Thanks.

    hi!
    I have been writing such programme recently. I used for-loop and if-then to search for the the name e.g:
    writeln('enter customers name');
    read(cusname);{store the name in a separate variable,maybe you have missed this step}
    for i:=1 to filesize(filevar) do{search thru the file}
    if cusname=file.field{field of the record which contains customers name}
    then...{what you what to do}
    I don`t know where your mistake is,but you should take a look at my code and if it`s not of help for you,I recommend you to post your code.
    I hope to have helped you.
    Looking forward to hearing from you
    li-zi
    :
    :

  • faraazfaraaz Posts: 15Member
    [b][red]This message was edited by faraaz at 2004-6-12 10:2:0[/red][/b][hr]
    [b][red]This message was edited by faraaz at 2004-6-12 10:0:8[/red][/b][hr]
    This procedure is for deleting.


    begin
    answer:='Y';
    assign(tempcust,'C:pascal~1 empcust.dat');
    rewrite(tempcust);
    reset(tempcust);
    while (answer='y') or (answer='Y') do
    begin
    write('Enter code of customer to delete:');
    readln(code);
    del:=0;
    rec:=0;
    seek(customer,rec);
    read(customer,delrec);
    while code <> delrec.custcode do
    begin
    rec:=rec+1;
    seek(customer,rec);
    if eof(customer) then
    begin
    writeln('Not found. Press any key');
    readln;
    close(customer);
    end;
    read(customer,delrec);
    end;
    with delrec do
    begin
    writeln('name: ',mname);
    writeln('address: ',address);
    writeln('tel: ',tel);
    writeln;
    end;
    writeln('Are you sure you want to delete(Y/N)?');
    read(delans);
    if (delans = 'y') or (delans='Y') then
    begin
    seek(customer,del);
    ====> while not eof(customer) do { {It's here that i
    begin { am having a problem now.
    read(customer,delrec); { i am getting a message
    if code <> delrec.custcode then { "Disk read error".
    begin { Please check if this piece
    write(tempcust,delrec); { of coding is good and
    del:=del+1; { correct if necessary.}
    seek(customer,del);
    if eof(customer) then
    begin {{Check the coding below for
    read(customer,delrec); { renaming tempcust.dat}
    write(tempcust,delrec); code:=delrec.custcode;
    end;
    read(customer,delrec);
    end;
    del:=del+1;
    seek(customer,del);
    end; <==================================================
    close(customer);
    erase(customer);
    close(tempcust);
    writeln('A new up-to-date file has been created containing your records');
    write('Enter a name for the file:');
    readln(filename);
    assign(tempcust,'C:pascal~1 empcust.dat');
    rename(tempcust,'C:pascal~1'+filename+'.dat');

    writeln('This record has been successfully deleted');
    writeln('Press any key');
    readln;
    end
    else
    begin
    writeln;
    write('Delete more customer(Y/N):');
    readln(answer);
    end;
    end;

    end;







  • bpajkbpajk Posts: 156Member
    : [b][red]This message was edited by faraaz at 2004-6-12 10:2:0[/red][/b][hr]
    : [b][red]This message was edited by faraaz at 2004-6-12 10:0:8[/red][/b][hr]
    : This procedure is for deleting.
    : [code]
    :
    : begin
    : answer:='Y';
    : assign(tempcust,'C:pascal~1 empcust.dat');
    : rewrite(tempcust);
    : reset(tempcust);
    : while (answer='y') or (answer='Y') do
    : begin
    : write('Enter code of customer to delete:');
    : readln(code);
    : del:=0;
    : rec:=0;
    : seek(customer,rec);
    : read(customer,delrec);
    : while code <> delrec.custcode do
    : begin
    : rec:=rec+1;
    : seek(customer,rec);
    : if eof(customer) then
    : begin
    : writeln('Not found. Press any key');
    : readln;
    : close(customer);
    : end;
    : read(customer,delrec);
    : end;
    : with delrec do
    : begin
    : writeln('name: ',mname);
    : writeln('address: ',address);
    : writeln('tel: ',tel);
    : writeln;
    : end;
    : writeln('Are you sure you want to delete(Y/N)?');
    : read(delans);
    : if (delans = 'y') or (delans='Y') then
    : begin
    : seek(customer,del);
    : ====> while not eof(customer) do { {It's here that i
    : begin { am having a problem now.
    : read(customer,delrec); { i am getting a message
    : if code <> delrec.custcode then { "Disk read error".
    : begin { Please check if this piece
    : write(tempcust,delrec); { of coding is good and
    : del:=del+1; { correct if necessary.}
    : seek(customer,del);
    : if eof(customer) then
    : begin {{Check the coding below for
    : read(customer,delrec); { renaming tempcust.dat}
    : write(tempcust,delrec); code:=delrec.custcode;
    : end;
    : read(customer,delrec);
    : end;
    : del:=del+1;
    : seek(customer,del);
    : end; <==================================================
    : close(customer);
    : erase(customer);
    : close(tempcust);
    : writeln('A new up-to-date file has been created containing your records');
    : write('Enter a name for the file:');
    : readln(filename);
    : assign(tempcust,'C:pascal~1 empcust.dat');
    : rename(tempcust,'C:pascal~1'+filename+'.dat');
    :
    : writeln('This record has been successfully deleted');
    : writeln('Press any key');
    : readln;
    : end
    : else
    : begin
    : writeln;
    : write('Delete more customer(Y/N):');
    : readln(answer);
    : end;
    : end;
    :
    : end;
    :
    : [/code]
    :
    :
    :
    :
    :
    :
    I see how you try to delete the record but it is not very efficient. And you use seek() too often and incorrect. Below you have a code based on a different principle. It copyes the data from the file into an array, removes the record from the array and dumps the array back into the file. Study it.

    PS: When using code tags use "[]" not "<>".

    [CODE]
    program sample;
    uses crt;
    type tPerson=record name: string;
    id: longint;
    end;
    var f: file of tPerson;
    person: tPerson;
    id: longint;
    persons: array [1..100] of tPerson;
    NumPersons: longint;
    select: char;

    {Add a person to the file.}
    procedure add;
    begin
    clrscr;
    reset(f);
    seek(f,filesize(f));
    write('Name: ');
    readln(person.name);
    write('ID:');
    readln(person.id);
    write(f,person)
    end;

    {Delete a person in the file by the persons id.}
    procedure del;
    var i: longint;
    begin
    clrscr;
    reset(f);
    {Put all the persons in an array.}
    NumPersons:=0;
    while not eof(f) do
    begin
    inc(NumPersons);
    read(f,persons[NumPersons])
    end;
    {Search for the person by the id in the array.}
    write('Enter ID: ');
    readln(id);
    i:=1;
    while (i<=NumPersons) and (persons[i].id<>id) do
    inc(i);
    {Delete a person from the array.}
    if id=persons[i].id then
    begin
    dec(NumPersons);
    for i:=i to NumPersons do
    persons[i]:=persons[i+1];
    {Dump the array into the file.}
    rewrite(f);
    for i:=1 to NumPersons do
    write(f,persons[i]);
    writeln('SUCCESS!')
    end
    else
    writeln('Person not found!');
    readln
    end;

    {Output all persons in the file.}
    procedure output;
    begin
    clrscr;
    reset(f);
    while not eof(f) do
    begin
    read(f, person);
    writeln(person.name);
    writeln(person.id);
    writeln
    end;
    readln;
    end;

    {MAIN}
    begin
    assign(f,'testdata.txt');
    repeat
    clrscr;
    writeln('(1) Add person');
    writeln('(2) Del person');
    writeln('(3) Output');
    writeln('(4) Exit');
    write('Select: ');
    readln(select);
    case select of
    '1': add;
    '2': del;
    '3': output
    end;
    until select='4';
    close(f)
    end.
    [/CODE]
Sign In or Register to comment.