• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

how can I edit the file?

I have a  student  record file and I want to know how can I edit
or change the content of one record without changing the whole file. I also want  the procedure to ask the user to edit only
the field that he likes to change.
for example studentrec contains field like name,address,telephone,etc.
and I want to change record number 3 ,field name only I want other things to remain the same without me typing it again.
0
der_23
Asked:
der_23
  • 5
  • 2
  • 2
  • +1
1 Solution
 
der_23Author Commented:
You should be able to give me clear explaination step by step
givng good example and not too complex
Thank you very much
0
 
jack_p50Commented:
To write file not damaging it contents (rewriting) you can just

type
  studentrec=record
    name:string[20];
    address:string[40];
    telephone:string[10];
  end;
var
  dat:studentrec;
  f:file;
  q:string;
begin
  assign(f,'students.dat');
  reset(f,1);  {sizeof returns size of any type in bytes}
  seek(f,sizeof(studentrec)*3);  {because you need record number 3}
  readln(dat.name);
  readln(dat.address);
  readln(dat.telephone);
  blockwrite(f,dat,sizeof(studentrec));
{if you need to write only i.e. address, you should :}
  seek(f,sizeof(studentrec)*3+sizeof(studentrec.name));{because you need to skip name                                  to go to the address; if you need only telephone, you should add                                  sizeof(studentrec.telephone) too}
  readln(dat.address);
  blockwrite(f,dat.address,sizeof(studentrec.address));
end.

All clear?

0
 
der_23Author Commented:
Well, I want the  user to get involved as to what field he wants to edit
The user should be able to input his choice.And also my file is not a
text file but it is a file of studentrec.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
vikiingCommented:
That's what Jack showed you: the way to deal with studentrec file, not a text one.

If you want to ask user which field he want modify, make something so simply like this:
Writeln('¿What field do you want modify?');
Write('1:Name  2:Address  3:Phone  :'); Readln(opt);

Case opt of
   1: Readln(dat.name);
   2: Readln(dat.address);
   3: Readln(dat.phone);
end;

Jack: ¿why you suggest to use a non-formatted file?; it's more error-prone than using "file of studentrec". With that, you only need Seek(f, <record number>);


0
 
der_23Author Commented:
PROCEDURE editdata;
  VAR
     rec : integer;
     opt:char;{fp is my file of studentrec};
  BEGIN
     clrscr;
     assign(fp, _FileName);
     reset(fp);
     writeln;
     write('Enter the rec no. of the record you wants to edit:  ');
     readln(rec);
seek(fp, rec-1); {because you need record number 3}
write('What field do you want to modify1,2,3,4,5,6);');
readln(opt);
write('change to:');
Case opt of
'1':readln(student.first);
'2':readln(student.last);
'3':readln(student.id);
'4':readln(student.address);
'5':readln(student.tel);
'6':readln(student.major);
end;
write(fp,student);
     writeln('Press <enter> to continue');
     close(fp);
  END;
 Thanks Viking but a little problem more :>
This is the procedure I have done in my program.I can get to the record that I want to modify but when I modify the other fields which I
have are los but the field that I modified is there.So do you have any Idea what I should do ?
                    Thanks a lot
                         der_23
0
 
jack_p50Commented:
i always preferred untyped files.
0
 
omsecCommented:
what about using a binary file structure and manage its items using VARIANT RECORDS ?
0
 
der_23Author Commented:
look at my above comment I have the procedure which is not running well
0
 
der_23Author Commented:
Adjusted points to 35
0
 
vikiingCommented:
The problem is you are not reading the record. Seek() instruction just puts the file pointer to point to that record, but DOES NOT read it. Thus, after Seek, you must:

seek(fp, rec-1); {because you need record number 3}
Read(fp, student);

Beside that, you must remember that Read() procedure moves the pointer one record ahead; therefore, after reading, you must re-establish the pointer in order to point to the same record read, with this:


Case opt of
   . . .
end;

Seek (fp, rec-1); write(fp,student);
     writeln('Press <enter> to continue');

If you read record, say, 4, and don't re-seek, Write operation will record modified contents on record 5, leaving record 4 unmodified and losing original contents of record 5 because it will have data pertaining to record 4.

And a last suggestion: Assign, Reset and Close instructions need not to be done for each record user wants modify. A more logical process would be:
1 - Assign & Reset
2 - Let user modifies as many records as he wants
3 - Close the file

That minimizes I/O operations, making process faster (althoug it has a certain possitive effect: changes done in the way you do are ALWAYS physically recorded on disk each time they occur. Following the last steps I've told you, the changes are buffered into memory. That implies that if you have a power fail during modifying process your method will loose changes of only 1 records; otherwise, you can loose many more).

Regards

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now