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);
}