Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how can I edit the file?

Posted on 1998-11-28
10
Medium Priority
?
171 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
[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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

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.

Question has a verified solution.

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

What monsters are hiding in your child's room? In this article I will share with you a tech horror story that could happen to anyone, along with some tips on how you can prevent it from happening to you.
Are you looking for the options available for exporting EDB files to PST? You may be confused as they are different in different Exchange versions. Here, I will discuss some options available.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 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