Solved

how can I edit the file?

Posted on 1998-11-28
10
158 Views
Last Modified: 2010-04-16
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
Comment
Question by:der_23
  • 5
  • 2
  • 2
  • +1
10 Comments
 

Author Comment

by:der_23
ID: 1216216
You should be able to give me clear explaination step by step
givng good example and not too complex
Thank you very much
0
 
LVL 4

Expert Comment

by:jack_p50
ID: 1216217
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
 

Author Comment

by:der_23
ID: 1216218
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
 
LVL 3

Expert Comment

by:vikiing
ID: 1216219
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
 

Author Comment

by:der_23
ID: 1216220
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:jack_p50
ID: 1216221
i always preferred untyped files.
0
 
LVL 2

Expert Comment

by:omsec
ID: 1216222
what about using a binary file structure and manage its items using VARIANT RECORDS ?
0
 

Author Comment

by:der_23
ID: 1216223
look at my above comment I have the procedure which is not running well
0
 

Author Comment

by:der_23
ID: 1216224
Adjusted points to 35
0
 
LVL 3

Accepted Solution

by:
vikiing earned 30 total points
ID: 1216225
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
We have come a long way with backup and data protection — from backing up to floppies, external drives, CDs, Blu-ray, flash drives, SSD drives, and now to the cloud.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now