Solved

how can I edit the file?

Posted on 1998-11-28
10
162 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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
 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unsigned int64 to unsigned int32 10 1,678
Creating a blank copy of a TClientDataSet 1 1,154
detect keyboard input monitoring 3 392
copy one tchart to another 1 528
When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
As a business owner, there are many things that keep you up at night. Profit margins, employee retention, human resource protocols, whether your product or service will remain competitive. When you own or manage a technology company that operates la…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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