Solved

how can I edit the file?

Posted on 1998-11-28
10
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Multi-threading long-running processes can have a significant increase in overall performance and drastically decrease over time it takes for a process to complete. Unfortunately, not all applications support native multi-threading, some by design a…
This article outlines some of the reasons why an email message gets flagged as spam on a recipient's end.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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