help on records - Programmers Heaven

Howdy, Stranger!

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

Categories

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.

help on records

darscute03darscute03 Posts: 12Member
i need to put a edit and a delete in this one here is the code i already have create,add,view. please help thanks


uses crt;

const
path = 'I:Question.Dat';

type
qa= record
question:string;
Answer:string;
c1:String;
c2:string;
c3:string;
end;
var qFile:File of Qa;
RecQ:QA;
Choice:Char;

Procedure Create;

var
a:integer;
begin
clrscr;
assign(qfile,path);
rewrite(qfile);
write('Please wait while writing file.');

for a:= 1 to 10 do
begin
write('.');
delay(50);
end;
writeln;
writeln('File was written and ready to use.');
writeln('Press any key to continue.');

close(qfile);
readln;
end;

Procedure Add;

begin
clrscr;
assign(qfile,path);
reset(qfile);
seek(qfile, filesize(qfile));
with recq do
begin
write('Your question:') ; readln(Question);
write('Your answer:'); readln(Answer);
write('A: '); readln(c1);
write('B: '); readln(c2);
write('C: '); readln(c3);
end;
write(qfile,recq);

end;
procedure view;
begin
clrscr;
assign(qfile,path);
reset(qfile);
while not eof(qfile) do
begin
read(qfile,recq);
with recq do
begin
writeln('Your Question:',Question);
writeln('Your Answer:',Answer);
writeln('A:',c1);
writeln('B:',c2);
writeln('C:',c3);
end;
writeln;
end;
readln;
end;

begin
repeat
clrscr;
gotoxy(29,3); writeln('[C] - Create File');
gotoxy(29,4); writeln('[A] - Add Records');
gotoxy(29,5); writeln('[E] - Edit Records');
gotoxy(29,6); writeln('[D] - Delete Records');
gotoxy(29,7); writeln('[V] - View Records');
gotoxy(29,8); writeln('[X] - Exit');
choice:= readkey;

case choice of

'C','c':Create;
'A','a':Add;
'V','v':View;
end;
until (choice = 'X') or (Choice = 'x')

end.



Comments

  • Phat NatPhat Nat Posts: 757Member
    To edit a record, seek() to it's position, read it, then make the changes, seek() to that same record position again and write it. It will just save it over top of the old one. How you want to find it is your choice. You could ask for the question number or just display the questions one-by-one until the user sees the one they want to edit or you could include a search to find any records relating to the search.

    As for deleting records, this is more intersting. You could just blank out the question line and have your program check for this (although it will add extra useless data to your file) or you could actually delete the record. To do this, open your file, rename it to a temp name, make a new file and copy every record except the one you want to delete, erase the temp file.

    [code]
    assign(newFile, path);
    rename(newFile, 'temp.tmp'); reset(newFile);
    assign(qfile, path); rewrite(qfile);
    x := 0;
    While not(eof(newFile)) Do
    Begin
    Inc(x);
    Read(newFile,recq);
    if x <> recordNumToDelete Then
    Write(qfile,recq);
    End;
    close(qfile);
    close(newFile);
    erase(newFile);
    [/code]

    Something along these lines anyways. Hope this helps.
  • darscute03darscute03 Posts: 12Member
    can you help me on edit since this is kind of hard for me thank you for your help
  • Phat NatPhat Nat Posts: 757Member
    First thing is I would make a procedure that all it does is saves the record to a given record number. In this example, if the given record number is -1, we assume that it is a new record and add it to the end.

    It is bad programming habit to leave a file open any longer than needed. So in the case of your "Add" procedure, the file is left open the whole time a user is inputting data. If this file was to be used by more than one person and a person just left their screen at the input, nobody else could use it. Also, if a computer crash happened, you could lose all of the info in the file. (Also, you were not closing the file)

    So first, let's modify your Add procedure:
    [code]
    Procedure Add;
    begin
    clrscr;
    with recq do
    begin
    write('Your question:') ; readln(Question);
    write('Your answer:'); readln(Answer);
    write('A: '); readln(c1);
    write('B: '); readln(c2);
    write('C: '); readln(c3);
    end;
    writeRecord(path, recq, -1);
    end;
    [/code]

    Okay. So now we need to write a procedure called "writeRecord()"
    [code]
    PROCEDURE writeRecord(fileName : String; recToSave : qa; recordNum : Integer);
    var qFile : File of Qa;
    Begin
    assign(qfile, fileName);
    reset(qfile);
    if (recordNum = -1) Then
    seek(qfile, filesize(qfile))
    else
    seek(qfile, recordNum);
    write(qfile, recToSave);
    close(qfile);
    End;
    [/code]

    Now we can save our data to any record we want. Make a similar procedure for reading:
    [code]
    FUNCTION readRecord(fileName : String; VAR recToRead : qa; recordNum : Integer) : Boolean;
    var qFile : File of Qa;
    Begin
    assign(qfile, fileName);
    reset(qfile);
    {$I-}
    seek(qfile, recordNum);
    {$I+}
    If IOResult = 0 Then
    Begin
    read(qfile, recToRead);
    readRecord := True;
    End
    ELSE
    readRecord := False;
    close(qfile);
    End;
    [/code]

    You'll notice I added a dew new lines. These check for any errors (such as asking for record #6 when there are only 5 records) and the function will return true if it could read it.

    to edit:
    [code]
    If (readRecord(path, recq, recordNum) = true) Then
    Begin
    writeLn('OLD QUESTION: '+recq.question);
    write ('NEW QUESTION: '); ReadLn(recq.question);
    {... etc, make your changes to recq ...}
    writeRecord(path, recq, recordNum);
    End;
    [/code]

    Also, try to keep your capitialization on variables the same throughout. you have your record declared as 'qa', then assign it as 'QA' and have a file of type 'Qa'. I know it doesn't matter in Pascal, but it's another good programming habit to get into.
  • darscute03darscute03 Posts: 12Member
    anyway can you tell me a command that can only overwrite seems if i use write(qfile,recq); in edit it just creates another record is there any command that can only overwrite the one you want to edit thanks for your help


    and if i use the commands you gave me it say writeRecord is unidentified variable what should i do with this...
  • darscute03darscute03 Posts: 12Member
    please help me on this i really need it thank you
  • Phat NatPhat Nat Posts: 757Member
    I wrote it above, did you try the code I posted?
    You have to Seek() to the record you want first. If you are at the end of the file, then it will create a new record.
  • darscute03darscute03 Posts: 12Member
    yup i tried it but there was an error that says writeRecord is a unknown identifier that was the error i could use the program well but that error...can you fix it what should i do with the writeRecord?


    writeRecord(path, recq, -1); (writeRecord)=is unknown identifier what should i do with it?
  • Phat NatPhat Nat Posts: 757Member
    Did you add the "PROCEDURE writeRecord(...);" from above to your code?
    If so, make sure that it is listed above any other procedures that call it (in this case the "Add" & the edit procedure). With Pascal a procedure that is called must have the definition come first.

    AKA make sure that the writeRecord() procedure is above the Add() procedure.
  • darscute03darscute03 Posts: 12Member
    ok men first of all i want to thank you for this and i want to explain this tell me if im wrong :D

    the function that you make holds and read the records the writeRecord is the one who saves and return the value on edit

    anyway thanks if im wrong can you explain it to me men you help me alot thank you
  • Phat NatPhat Nat Posts: 757Member
    The readRecord() procedure asks for the number of a record and stores just that one record in memory, no others. The writeRecord() procedure just changes the information of a record.

    Think of it like a recipe box with cue cards all numbered in order.
    If we look at card number 16, maybe we'll see that it is a recipe for chocolate chip cookies. We don't know what any other card is, although we could look at them if we wanted. This is how our readRecord() works. We just provide a number and get back the info. If we wanted a certain recipe, we would have to "look at" or "readRecord()" all cards until we found the one we wanted.

    Now that we are looking at #16 let's say that we find a mistake. We can take it out, make changes and put it back in place (our writeRecord() procedure).

    Now let's say that we want to write a new card. We find out that we have 31 recipes, so we just writeRecord() to recipe card #32 (our "Add()" procedure).

    Hopefully this explains it a bit (or makes you hungary).
Sign In or Register to comment.