Hi ,

i know one method of deleting a record from a binary file.

that is :

read the contents of the file and every time check for some criteria based on which we want to delete the record.

if record does not match criteria then write to new file if it matches then dont write that record continue with the next record.

i have written a program for this and is working fine.

but othe method : tried opening a file in rb+ mode and read the file till i find the record when i find the record , going to next record and write that record in previous place and next next record in next prev place and so on as below but its not working. please help me.

void Delete_Player( char *name )
{
        FILE *infp , *outfp ;
        int nor, wchfile, found = 0 ;
        Player_t P, temp ;

        infp = fopen ("PLAYERsInfo.dat","rb+");
        //outfp = fopen("Dup.dat","wb");

         if ( !infp ) {
                printf (" ( \"%s\" ) open failed \n" , "PLAYERsinfo.dat");
                return ;
        }

        while (1 == ( nor = fread (&P , sizeof ( P ), 1 , infp) ) )
        {
                if ( ! strcmp ( name , P.name ) ) {
                        found = 1 ;
                        break ;
                }
        nor++;  //fwrite ( &P , sizeof (P), 1 , outfp );
        }

        if ( feof ( infp ) )
        {
                printf("No ( \" %s \") is present \n", name );
                return ;
        }

        while ( ! feof( infp) )
        {
                fseek( infp, sizeof(P) * nor , SEEK_SET );
                fread ( &P, sizeof ( P ), 1 , infp );
                fseek( infp, sizeof(P) * ( nor - 1 ) , SEEK_SET );
                fwrite ( &P, sizeof ( P ), 1, infp );
                nor++;
        }
 printf(" ( \" %s \" ) is deleted \n", name );

        fclose(infp);
}

Hi ,

i know one method of deleting a record from a binary file.

that is :

read the contents of the file and every time check for some criteria based on which we want to delete the record.

if record does not match criteria then write to new file if it matches then dont write that record continue with the next record.

i have written a program for this and is working fine.

but othe method : tried opening a file in rb+ mode and read the file till i find the record when i find the record , going to next record and write that record in previous place and next next record in next prev place and so on as below but its not working. please help me.

void Delete_Player( char *name )
{
        FILE *infp , *outfp ;
        int nor, wchfile, found = 0 ;
        Player_t P, temp ;

        infp = fopen ("PLAYERsInfo.dat","rb+");
        //outfp = fopen("Dup.dat","wb");

         if ( !infp ) {
                printf (" ( \"%s\" ) open failed \n" , "PLAYERsinfo.dat");
                return ;
        }

        while (1 == ( nor = fread (&P , sizeof ( P ), 1 , infp) ) )
        {
                if ( ! strcmp ( name , P.name ) ) {
                        cnt_rec++;
                        break ;
                }
       //nor++;( got it invalid) ( corrected )
       cnt_rec++;//fwrite ( &P , sizeof (P), 1 , outfp );
        }

        if ( feof ( infp ) )
        {
                printf("No ( \" %s \") is present \n", name );
                return ;
        }

        while ( ! feof( infp) ) // entering infinite dont know cause
        {
                fread ( &P, sizeof ( P ), 1 , infp );
                cnt_rec++;
                fseek( infp, sizeof(P) * ( cnt_rec - 2 ) , SEEK_SET );
                fwrite ( &P, sizeof ( P ), 1, infp );
                fseek( infp, sizeof(P) * cnt_rec , SEEK_SET );
        }
 printf(" ( \" %s \" ) is deleted \n", name );

        fclose(infp);
}

have done some changes last post was having some error.

Should you be using SEEK_SET in this loop maybe try using SEEK_CUR

while ( ! feof( infp) ) // entering infinite dont know cause
{
fread ( &P, sizeof ( P ), 1 , infp );
cnt_rec++;
fseek( infp, sizeof(P) * ( cnt_rec - 2 ) , SEEK_SET );
fwrite ( &P, sizeof ( P ), 1, infp );
fseek( infp, sizeof(P) * cnt_rec , SEEK_SET );
}

Well for one thing, you're not checking the return result of all the file access functions.
At the end of the file, the fread will NOT advance by the size of 1 record, but your following seek doesn't know that.

Result - trashed last record (probably).

please upload this code in vc++ language

You don't want to over-write records and shuffle them around a lot - that's risky business, and unnecessary.

Instead, just mark the name or a record number (like an id number with students), as zero or '\0'.

Now, your program can tell that record has been deleted, and is available for editing with new record data, as you add your next record.

That saves a lot of dangerous data shuffling and overwriting, as well.

If you wind up with too many deleted records this way, your program will just write out all the non-deleted records, to a new record file, and you're good to go.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.