Closed Thread
Results 1 to 2 of 2

Thread: Not overwriting

  1. #1
    hbk
    hbk is offline Learning Programmer
    Join Date
    Mar 2010
    Posts
    68
    Rep Power
    0

    Not overwriting

    I am passing three char parameter to a function below with a desired input but when i can scan the data from the file and then reprinting to the file with the updated data its not carrying out the diserd output to the file. instead it prints back the information at the end of the record.
    Code:
    void update(char * record1,char *record2,char *record3)
    {
      system("color 2"); 
      
       
      int tell=0;
      int Found=0;
      long int offset = -1;
      char treat[30];
      char app[30];
         
                   
                  if((customer1=fopen("customer1.txt","r+"))==NULL)
                       printf("THE FILE IS EMPTY");
                  else
                  {
                                     
                      while(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)==8
                        && Found==0)
                          {
                            offset = ftell(customer1);                        
                            //fscanf(customer1,"%s %s %s %s %s %s %s %s\n",info.regis,info.Name,info.address,info.number,info.dob,info.treatment,info.allergies,info.app);
                   
                            if(strcmp(record1,info.regis)==0)
                            Found=1;
                          }
                         
                          if(Found)
                          {            
                                                   
                            strcpy(treat,record2);
                            strcpy(app,record3);   
                             
                            fseek(customer1,offset,SEEK_SET);                                     
                                                
                            fprintf(customer1,"%s %s %s %s %s %s %s %s ",info.regis,info.Name,info.address,info.number,info.dob,treat,info.allergies,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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Not overwriting

    Thats because you call fseek(customer1, offset, SEEK_SET) while your offset is initiated by ftell after having scanned the whole file. So after the last loop iteration, offset will indicate the end of the data in file.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Overwriting a distro?
    By EGS in forum Linux Installation & Configuration
    Replies: 9
    Last Post: 03-28-2009, 09:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts