Deleting with "fwrite()" 
Author Message
 Deleting with "fwrite()"

I have a question on using fwrite() to delete information from a database.  I
have gotten help here before, and appreciate any info you can send my way.  If
it will ease the situation, THIS IS NOT A HOMEWORK ASSIGNMENT!  I am not going
to school.  Even though after some of you look at this, you'll probably say
I should! :-)

i am trying to use the fwrite() function to remove information from a database.
Can you "re-write" over the top of a sector stored in the database by fwriting
over the top of it?  Below is what I have come up with, but the information I
wish to delete is still remains in the database.  Again, thanks for any help
you can send my way.  I have found that the subject of deleting information
from a database, is hard to come by in text books.

I have defined in the structure addr, char delete;

/* remove a name from the list */
void delete()
{
        FILE *fptr;
        char name[40];
        struct addr addr_info;

        if((fptr=fopen("maillist.dbf","rb"))==NULL) {
                fprintf(stderr,"Can't open, name not found.\n");
                return;
        }

        printf("\n\n\n\n\n\n\t\t\tName your looking for? ");
        gets(name);

        while(!feof(fptr)) {
        fread(&addr_info,sizeof(struct addr),1,fptr);
                if(!strcmp(addr_info.name, name)) {
         printf("\n\n\n\n\n\n\t\t\t\t%s\n", &addr_info.name);
                        printf("\t\t\t\t%s\n", &addr_info.street);
                        printf("\t\t\t\t%s\n", &addr_info.city);
                        printf("\t\t\t\t%s\n", &addr_info.state);
                        printf("\t\t\t\t%s\n", &addr_info.zip);
                        printf("\n\t\tIs this the name you want to remove? (Type 'y' or 'n'):  ");
         while(getche() == 'y');
           addr_info.delete='y';
              fwrite(&addr_info,sizeof(struct addr),1,fptr);
           clrscr();
         return;
      }
        }
        fclose(fptr);

Quote:
}

--
        _**_                   "We came...We saw...We kicked some ASH!"
       /____|-IIIIIIIIIIII      Gary Thurman ------ FIREFIGHTER/EMT 2

      +-(O)--------(O)--+       Corvallis, Oregon


Mon, 18 Oct 1993 01:16:05 GMT  
 Deleting with "fwrite()"

Quote:

>i am trying to use the fwrite() function to remove information from a database.
>Can you "re-write" over the top of a sector stored in the database by fwriting
>over the top of it?

Yes.  Your program below has three basic misunderstandings of how to use
stdio.  (there's actually a number of small problems in the program, I'll
just deal with the stdio problem)

First, you opened the file with mode "rb", meaning "readonly, binary format".
If you want to change the file using the same stdio stream, you must use "r+b"
or "rb+", meaning "open for reading and writing, binary format".

Second, the position at which IO happens is usually named the "file pointer"
(not to be confused with "FILE pointer"); each IO operation advances the file
pointer by the amount of IO performed, so when your application tries to do

        fread(buffer, sizeof(buffer), 1, stream);
        ...
        fwrite(buffer, sizeof(buffer), 1, stream);

you are not trying to overwrite the intended record, you are trying to
overwrite the next record.  This is the second misunderstanding.

Lastly, a read-write stdio stream cannot be used for "intermingled" reading
and writing:  in order to switch from reading to writing (or vice versa),
you must do an fseek() or rewind().  (It really isn't hard to write a stdio
package that gets this right, but AT&T didn't, and now it is cast in stone
for all time by ANSI)

As it happens, fixing your second problem (didn't back up to the beginning
of the record) fixes the third problem anyway, since (of course) you do that
with fseek().  The skeleton of what you want is, therefore,

        long cookie;

        cookie = ftell(stream);
        fread(record, sizeof(record), 1, stream);

        if /* smash it */ {
                fseek(stream, cookie, SEEK_SET);
                fwrite(record, sizeof(record), 1, stream);
        }



Mon, 18 Oct 1993 03:41:53 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. fwrite and "bad address" message

2. fwrite man page "error"

3. Why can only undo "Delete"?

4. Delete "/" from char pointer

5. ODBC, VFP and "real" deletes

6. how to use "delete"?

7. "delete []" in MSDN

8. Question about the operator "delete"

9. "scalar deleting destructor"

10. C++ question with "delete"

11. Error when "delete[] m_pHashTable" in CMap

12. SUMMARY: "Deleting every other byte"

 

 
Powered by phpBB® Forum Software