#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Fseek() and overwriting data


    I am trying to use fseek to overwrite data in a file , the code works but its printing the new information to be written at the end of the line in which the data is found.

    Code:
    void updaterecord(void)
    {
         
       system("color 2");
       
       struct custinfo info;
       
       FILE *customer1;   
        
       
       customer1=fopen("customer1.txt","r");
       
       if (customer1==NULL)
                printf("\n\n\t\t\t  FILE WAS NOT FOUND ");
        else
             {
                  char choice4; 
                  char Target[50];
                  int Found=0;
                  
                  
                  if((customer1=fopen("customer1.txt","r+"))==NULL)
                       printf("THE FILE IS EMPTY");
                  else
                  {
                      printf("\n\n\t\t ENTER ID NUM:");
                      scanf("%s",&Target);
                      while(!feof(customer1)&& Found==0)
                          {
                            
                            fseek(customer1,0L,SEEK_SET);
                                        
                            fscanf(customer1,"%s %s %s %s %s %s %s %s",info.regis,info.Name,info.address,info.number,info.dob,info.treatment,info.allergies,info.app);
                   
                            if(strcmp(Target,info.regis)==0)
                            Found=1;
                          }
                         
                          if(Found)
                          {                                              
                               
                            printf("\n\t\t  REGISTRATION:%s\n",info.regis);
                            printf("\n\t\t  NAME:%s\n",info.Name);
                            printf("\n\t\t  ADDRESS:%s\n",info.address);
                            printf("\n\t\t  NUMBER:%s\n",info.number);                       
                            printf("\n\t\t  DATE OF BIRTH:%s\n",info.dob); 
                            printf("\n\t\t  LAST TREATMENT:%s\n",info.treatment); 
                            printf("\n\t\t  ALLERGIES:%s\n",info.allergies);    
                            printf("\n\t\t  LAST APPOINTMENT:%s\n",info.app);             
                                                  
                           
                            printf("\n\n\t\t ENTER THE NEW DATA BELOW:");                   
                                     
                            printf("\n\n\t\t\t  NAME: ");
                            fflush(stdin);
                            gets(info.Name);     
         
                            printf("\n\n\t\t\t  ADDRESS: ");
                            gets(info.address);
         
                            printf("\n\n\t\t\t  NUMBER: ");
                            gets(info.number);
         
                            printf("\n\n\t\t\t  TREATMENT: ");
                            gets(info.treatment);
         
                            printf("\n\n\t\t\t  ALLERGIES: ");
                            gets(info.allergies); 
         
                            printf("\n\n\t\t\t  DATE OF BIRTH 12/12/12: "); 
                            gets(info.dob);     
            
                            printf("\n\n\t\t\t  DATE OF LAST APPOINTMENT 12/12/12: ");
                            gets(info.app);                     
                            
                            fseek(customer1,0,SEEK_CUR);              
                            
                            
                            fprintf(customer1,"%s %s %s %s %s %s %s %s ",info.regis,info.Name,info.address,info.number,info.dob,info.treatment,info.allergies,info.app);
                            
                            printf("\n\n\t\t  THE DETAILS OF THE ITEM HAS BEEN UPDATED SUCCESSFULLY"); 
                               
                            rewind(customer1);                 
                           
                           }
                            else 
                            if(!Found)
                             printf("\n\n\t\t THERES NO SUCH RECORD");
                           }
               
              
                 
             }
           
            
         
      
          fclose(customer1);             
         
                           
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,489
    Rep Power
    1876
    Well anyone using gets() and fflush(stdin) has got bigger problems to solve first.


    > customer1=fopen("customer1.txt","r");
    ...
    > customer1=fopen("customer1.txt","r+")
    You didn't close it before trying to reopen it.


    Plus, and this is critical, trying to do updates on a text file is broken. Trying to replace "Fred" with "Barney" does not magically make room in the file for the additional characters. Instead, it just overwrites things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo