c - All content of file get duplicated while modifying a specific entry -


this code read/write data to/from binary file. here modify() take input user, search in file. if found, prompt user give new entry , older entry replaced.

the question is, in modify() after fwrite() if break while loop fine but, if don't entry still modified along content of file duplicated, why ?

as in beginning there no duplicate entries in file. so expect is, if don't use break loop should go on , terminate when whole file has been read. here, if(strcmp(e.name,user)==0) true 1 entry therefore control should enter once in if block. how entries getting duplicated ?

    #include<stdio.h>     #include<stdlib.h>     #include<string.h>      void add(file *);     void list(file *);     void modify(file *);      struct emp     {         char name[20];         int id;         float sal;     }e;      void add(file *f)     {         char *p=null;          printf("\nenter name id sal\n");         scanf(" %s %d %f", e.name,&e.id,&e.sal);          fseek(f,0,seek_end);         if((fwrite(&e,sizeof(struct emp),1,f))==1)         {             printf("\nadded successfully\n");         }         else         {             printf("\nerror wrting file in add func\n");         }     }      void list(file *f)     {         rewind(f);         while(fread(&e,sizeof(struct emp),1,f)>0)         {             printf("\nread %s %d %f\n",e.name,e.id,e.sal);         }     }      void modify(file *f)     {         char user[20];         char *p=null;          printf("\nenter name modify\n");         scanf(" %s", user);          rewind(f);          while(fread(&e,sizeof(struct emp),1,f)==1)         {             //printf("\n        --------------- %s %d %f\n",e.name,e.id,e.sal);             if(strcmp(e.name,user)==0)             {                 //fseek(f,-sizeof(struct emp),seek_cur);                 printf("\nenter new name id salary\n");                 scanf(" %s %d %f", e.name,&e.id,&e.sal);                  fseek(f,-sizeof(struct emp),seek_cur);                 if(fwrite(&e,sizeof(struct emp),1,f)==1)                 {                     printf("\nmodified successfull!!\n");                     //break;                 }                 else                 {                     printf("\nerror while modifying\n");                 }             }             else             {                 printf("\n\nstring not matched\n\n");             }         }      }      int main()     {         char val='t';         file *fp=null;          if((fp=fopen("database.dat","rb+"))==null)         {             if((fp=fopen("database.dat","wb+"))==null)             {                 printf("\nerror opening file in wb+ mode\n");                 exit(0);             }         }                  {             printf("\nenter add, l list, d delete, m modify , e exit\n");             scanf(" %c", &val);              switch(val)             {                 case 'a':                         add(fp);                         break;                 case 'l':                         list(fp);                         break;                 case 'm':                         modify(fp);                         break;                 case 'd':     //                    del(fp);                         break;                 case 'e':                         fclose(fp);                         exit(0);                         break;                 default:                         printf("\ninvalid input\n");                         break;             }          }         while(1);      } 

i think it's problem between fread() , fwrite(). after use fwrite() modify emp, continue while loop without using fseek(). in fact, before, used fseek() go , ok. have place fseek() doesn't move, this:

fseek(f, 0, seek_cur); 

now can remove break , modify() function seems this:

void modify(file *f) {     char user[20];     printf("\nenter name modify\n");     scanf("%s",user);     fflush(stdin);     rewind(f);      while(fread(&e,sizeof(struct emp),1,f)==1)     {         if(strcmp(e.name,user)==0)         {             printf("\nenter new name id salary\n");             scanf("%s %d %f",e.name,&e.id,&e.sal);             fflush(stdin);              fseek(f,-sizeof(struct emp),seek_cur);             if(fwrite(&e,sizeof(struct emp),1,f)==1)             {                 printf("\nmodified successfull!!\n");                 //break; //this can removed                  fseek(f, 0, seek_cur); //place here             }             else             {                 printf("\nerror while modifying\n");             }         }         else         {             printf("\n\nstring not matched\n\n");         }     } } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -