Start New Discussion within our Software Development Community

i couldnt understand how fseek(fp,-size,SEEK_CUR) helps to allocate the modified data
at the location where name are found to be same. it would be great help how fseek points to the location where
data is going to modified. -size if fseek logic of pointing to the location where to modify data

while(fread(&e,size,1,fp)==1)
{
      if(strcmp(e.name,empname)==0){
                                                  printf("enter name age and salery\n");
                                                  scanf("%s%d%d",e.name,&e.age,&e.bs);
                                                  
                                                  fseek(fp,-size,SEEK_CUR);
                                                  fwrite(&e,size,1,fp);
                                                  break;
   }
}

i couldnt understand how fseek(fp,-size,SEEK_CUR) helps to allocate the modified data
at the location where name are found to be same. it would be great help how fseek points to the location where
data is going to modified.

fseek() doesn't allocate anything. It places the file pointer at the specified location in the file. Reading will then return the data at that precise position.

As for writing, it's very dangerous.

-size if fseek logic of pointing to the location where to modify data

Not a clue what you are asking.

Here's your code -- commented

while(fread(&e,size,1,fp)==1) // read SIZE bytes from the file into e
{
    if(strcmp(e.name,empname)==0) // if the name is the same
    {
        printf("enter name age and salery\n"); // read new data from user
        scanf("%s%d%d",e.name,&e.age,&e.bs);
                                                  
        fseek(fp,-size,SEEK_CUR); // back up in the file to the beginning of the record just read
        fwrite(&e,size,1,fp); // write the new record over the old
        break;
   }
}
This article has been dead for over six months. Start a new discussion instead.