?
Solved

searching and deleting directly from a file

Posted on 2000-04-07
9
Medium Priority
?
215 Views
Last Modified: 2010-04-16
Hi,

I have set up a file with an array of records. I managed to figure out how to search for the record I'm looking for, but now I am having trouble deleting directly from the file. Can anyone help?

Thanx,

Yael
0
Comment
Question by:yaelie
[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
  • 3
  • 2
  • 2
  • +2
9 Comments
 

Expert Comment

by:thunderchopper
ID: 2693872
If the record is in the middle of the file, you can't delete it straight away. The workaround is to load the file into memory, and then write it back to disk while omitting the record to be deleted.
0
 

Expert Comment

by:mathought
ID: 2694894
There are two methods for changing the contents of a file:
1) Rewriting the file
2) Using direct access to the disk using OS function according to the file's offset in the disk

The second option does not allow deleting data from the file thus you must resort to the first option.
0
 
LVL 49

Accepted Solution

by:
dbrunton earned 400 total points
ID: 2696613
thunderchopper's method works if the file is small.

If you don't know the file size then do the following.

open file(old records) for reading only --> this is your present file
open file(new records) for writing   --> totally new file
while not eof(old records)
  begin
     read(record, old records);
     compare(record, record_to_delete)
     if compare was not equal then
        write(record, new records)    --> write record if it is not one to delete
  end
close file(old records)
close file(new records)
delete file(old records)
rename file(new records to old records)
open file(old records)
0
Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

 
LVL 1

Author Comment

by:yaelie
ID: 2697694
Adjusted points from 50 to 100
0
 
LVL 1

Author Comment

by:yaelie
ID: 2697695
mathought thanx for the input but i think ill keep this question open for the time because you didnt really offer me an answer which helped me solve the problem. thanx anyways.


dbrunton (hi again!!) i tried constructing what you told me to but it doesnt seem to be working. The records have been defined before and the files MTEXTFILE and MTEMPFILE are already open.
OutputEntireMemberList is a procedure found before this one which simply goes through the file and lists all the records.
any idea how i can fix this?

thanx a bunch!

                  
Procedure DeleteAMember;
var counter:integer;
begin
      reset (MTEXTFILE);
      rewrite(MTEMPFILE);
      notfound:=true;
      writeln('Enter the name of the member you wish to delete');
      readln(MEMBERNAME);
      writeln;
      for counter:= 1 to size do
            begin
                  writeln('Reading in record number : ',counter);
                  readln(MTEXTFILE, MEMBER[COUNTER].NAME);      
                  readln(MTEXTFILE, MEMBER[COUNTER].DATEOFBIRTH);
                  readln(MTEXTFILE, MEMBER[COUNTER].SEX);
                  readln(MTEXTFILE, MEMBER[COUNTER].ADDRESS);
                  readln(MTEXTFILE, MEMBER[COUNTER].PHONE);
                  readln(MTEXTFILE, MEMBER[COUNTER].CCARD);      
                  if MEMBERNAME<>(MEMBER[COUNTER].NAME) then
                  { requested member is not found so write the member to the new file }
                        begin
                              FLAG:=true;
                              writeln('Writing member to new file');
                              writeln(MTEMPFILE, MEMBER[COUNTER].NAME);
                              writeln(MTEMPFILE, MEMBER[COUNTER].DATEOFBIRTH);
                              writeln(MTEMPFILE, MEMBER[COUNTER].SEX);
                              writeln(MTEMPFILE, MEMBER[COUNTER].ADDRESS);
                              writeln(MTEMPFILE, MEMBER[COUNTER].PHONE);
                              writeln(MTEMPFILE, MEMBER[COUNTER].CCARD);
                        end;
                        
                  {else      member IS found so ask for confirmation to delete
                        begin
                              FLAG:=true;
                              writeln('Are you sure you want to delete ',MEMBERNAME, '?');
                              readln(CHOICE);
                              writeln(CHOICE);
                              if (CHOICE='n') or (CHOICE='N') then
                                    { person changed their mind and doesnt want to delete
                                    begin
                                          writeln('CHOICE was : ',choice,' so writing to the file');
                                          writeln(MTEMPFILE, MEMBER[COUNTER].NAME);
                                          writeln(MTEMPFILE, MEMBER[COUNTER].DATEOFBIRTH);
                                          writeln(MTEMPFILE, MEMBER[COUNTER].SEX);
                                          writeln(MTEMPFILE, MEMBER[COUNTER].ADDRESS);
                                          writeln(MTEMPFILE, MEMBER[COUNTER].PHONE);
                                          writeln(MTEMPFILE, MEMBER[COUNTER].CCARD);
                                    end;
                        end;}
            end;                  
      if FLAG=false then
            begin
                  writeln;
                  writeln('Sorry, that member was not found.');                  
                  DeleteAMember;
            end;
      //clearing up text file//
      rewrite(MTEXTFILE);
      for counter:=1 to size-1 do
            begin
                  readln(MTEMPFILE, MEMBER[COUNTER].NAME);
                  writeln(MTEXTFILE, MEMBER[COUNTER].NAME);
                  readln(MTEMPFILE, MEMBER[COUNTER].DATEOFBIRTH);
                  writeln(MTEXTFILE, MEMBER[COUNTER].DATEOFBIRTH);
                  readln(MTEMPFILE, MEMBER[COUNTER].SEX);
                  writeln(MTEXTFILE, MEMBER[COUNTER].SEX);
                  readln(MTEMPFILE, MEMBER[COUNTER].ADDRESS);
                  writeln(MTEXTFILE, MEMBER[COUNTER].ADDRESS);
                  readln(MTEMPFILE, MEMBER[COUNTER].PHONE);
                  writeln(MTEXTFILE, MEMBER[COUNTER].PHONE);
                  readln(MTEMPFILE, MEMBER[COUNTER].CCARD);
                  writeln(MTEXTFILE, MEMBER[COUNTER].CCARD);
            end;
      OutputEntireMemberList;
      
end;      
0
 

Expert Comment

by:mathought
ID: 2698110
That's fine. I actually ment it to be a commet (naturly) but I found out a second-too-late.
never mind.
0
 
LVL 49

Expert Comment

by:dbrunton
ID: 2701176
I think all of the elements you want are in here.  You'll have to compare carefully with your code to see the differences.

Procedure DeleteAMember;
var counter : integer;
  begin
    reset (MTEXTFILE);
    rewrite(MTEMPFILE);
    Flag := false;    {* changed this *} { if FLAG is false then member not found }
    writeln('Enter the name of the member you wish to delete');
    readln(MEMBERNAME);
    writeln;
    for counter:= 1 to size do
      begin
        writeln('Reading in record number : ',counter);
        readln(MTEXTFILE, MEMBER[COUNTER].NAME);
        readln(MTEXTFILE, MEMBER[COUNTER].DATEOFBIRTH);
        readln(MTEXTFILE, MEMBER[COUNTER].SEX);
        readln(MTEXTFILE, MEMBER[COUNTER].ADDRESS);
        readln(MTEXTFILE, MEMBER[COUNTER].PHONE);
        readln(MTEXTFILE, MEMBER[COUNTER].CCARD);
        if MEMBERNAME <> (MEMBER[COUNTER].NAME) then
          { requested member is not found so write the member to the new file }
          begin
            FLAG := false;
            writeln('Writing member to new file');
            writeln(MTEMPFILE, MEMBER[COUNTER].NAME);
            writeln(MTEMPFILE, MEMBER[COUNTER].DATEOFBIRTH);
            writeln(MTEMPFILE, MEMBER[COUNTER].SEX);
            writeln(MTEMPFILE, MEMBER[COUNTER].ADDRESS);
            writeln(MTEMPFILE, MEMBER[COUNTER].PHONE);
            writeln(MTEMPFILE, MEMBER[COUNTER].CCARD);
          end
        else
        { else member IS found so ask for confirmation to delete }
          begin
            FLAG := true;
            writeln('Are you sure you want to delete ',MEMBERNAME, '?');
            readln(CHOICE);
            writeln(CHOICE);
            if (CHOICE='n') or (CHOICE='N') then
       { person changed their mind and doesnt want to delete }
              begin
                FLAG := false;   {* changed mind *}
                writeln('CHOICE was : ',choice,' so writing to the file');
                writeln(MTEMPFILE, MEMBER[COUNTER].NAME);
                writeln(MTEMPFILE, MEMBER[COUNTER].DATEOFBIRTH);
                writeln(MTEMPFILE, MEMBER[COUNTER].SEX);
                writeln(MTEMPFILE, MEMBER[COUNTER].ADDRESS);
                writeln(MTEMPFILE, MEMBER[COUNTER].PHONE);
                writeln(MTEMPFILE, MEMBER[COUNTER].CCARD);
              end;
          end;
      end;
end;


procdure CheckFlag;
begin
  if FLAG = false then
    begin
      writeln;
      writeln('Sorry, that member was not found.');
      DeleteAMember;
end;


//clearing up text file//
procedure CleanUp;
begin
   reset(MTEMPFILE);  { need to go back to the beginning on this one }
   rewrite(MTEXTFILE);
   for counter:= 1 to size - 1 do
     begin
       readln(MTEMPFILE, MEMBER[COUNTER].NAME);
       writeln(MTEXTFILE, MEMBER[COUNTER].NAME);
       readln(MTEMPFILE, MEMBER[COUNTER].DATEOFBIRTH);
       writeln(MTEXTFILE, MEMBER[COUNTER].DATEOFBIRTH);
       readln(MTEMPFILE, MEMBER[COUNTER].SEX);
       writeln(MTEXTFILE, MEMBER[COUNTER].SEX);
       readln(MTEMPFILE, MEMBER[COUNTER].ADDRESS);
       writeln(MTEXTFILE, MEMBER[COUNTER].ADDRESS);
       readln(MTEMPFILE, MEMBER[COUNTER].PHONE);
       writeln(MTEXTFILE, MEMBER[COUNTER].PHONE);
       readln(MTEMPFILE, MEMBER[COUNTER].CCARD);
       writeln(MTEXTFILE, MEMBER[COUNTER].CCARD);
     end;
   OutputEntireMemberList;
end;
0
 

Expert Comment

by:Ykoorb
ID: 2763600
to be honest i'm not really sure whether this has already come up in one of the previous answers - i scanned over quickly and couldn't see it though - i've actually come across the problem of deleting the records myself and i just sort of virtually deleted them.

Instead of re-organising the file of records and having to resave the file to disk which could be time consuming in larger files, why not had an extra field to your record structure called something like 'exist'. When you then want to delete a record you can set the records 'exist' field to false. Whenever you try to access a record you can then first check whether the record is within the file limits and then check whether its 'exist' field is true or false. If its true you can view the details if its false the user is told that the record doesn't exist.

Now the problem with this method is that when you delete a record you don't actually delete it, you just leave the record where it is in a dorment state and therefor your file will never really decrease in size once you have increased it in size.

To get round this you could introduce a system where say once every so many times the program has been run (store a value in a file somewhere incremented each runtime) the file is loaded into main memory, and one by one the files are checked to see whether they exist, if they do then they are copied to a new file, if not then they are just left out of the copying process and are -in effect - deleted.
 
             or

To get round the problem another way, if you don't want to reorganise your file every now and then (which i actually reccomend) you could do this :-
This answer kind of depends whether or not you have initially defined all the records and files e.g created them all but with blank data at initialisation stage) If you haven't created them all then this method could work, otherwise its the previous method for you. When you or the user tries to excede the size of the file by adding a new record which is greater than the filesize  (SEEK (file,FILESIZE(file)+1) instead of just allowing the file to be created you could first goto the beginning of the file and sequentially go through all stored records and check whether there are any deleted records. If a deleted record is found then the new data could be stored over the old data. This still means that the size of the file will never get smaller but at least you are using your space more efficiently without having to rewrite the file as frequently.

Hope there was something you wanted there. I think the answer is clear enough not to need algorithms but if i have explained something badly tell me and i'll add examples of application.





0
 
LVL 1

Author Comment

by:yaelie
ID: 2769035
Ykoorb, thank you very much for your input.. I decided to follow dbruntons instructions after all because i want
to actually delete the record directly, ie when you select delete it is taken off.
YOur idea is great, and if I do deal with larger files in the future I will follow it. For now, as my file is
pretty small.. I think the rewrite although tiresome, will work nicely.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 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