Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Help with fseek

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

    Help with fseek

    Hey im new to this forum and i have a piece of code in which i use fseek to try and update a particular record. It does work but when i seek the record its writing the new data at the end of the line of the data i want to overwrite.Any help is appreciated.

    Code:
    void updaterecord(void)
    {
         
       system("color 2");
       
       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)
                          {
                            
                            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,-0L,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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Help with fseek

    Code:
    fseek(customer1,-0L,SEEK_CUR);
    This doesn't go to the beginning of the line. It actually doesn't do anything. You're telling it to move zero bytes from the current position which is...the current position. You're going to have to figure out some other way to keep track of the beginning of the line.

    I also found a few other things wrong with your code:
    Code:
    if((customer1=fopen("customer1.txt","r+"))==NULL)
                       printf("THE FILE IS EMPTY");
    Not true. If you get NULL back that means that there was an error opening the file, most likely that it doesn't exist. This is different than an empty file, which exists but doesn't have anything in it.

    Code:
    fflush(stdin);
    Do not do this. It's undefined behavior by the ANSI C standard, which means it won't work everywhere. Some compilers let you get away with it, others won't and you're not going to get the expected results. Figure out some other way, like looping through fgetc until you hit a newline. (I'm sure there's a better way but I can't remember what right now.)

    Code:
    gets(info.Name);
    Don't ever use gets(). You can easily get a buffer overrun and blow away your own code, or hackers could exploit this and overwrite your code with their own, if they want. (This is how Internet Explorer used to get hacked so easily. It had a lot of these.) Use fgets(info.Name, length_of_Name, stdin) instead. Much safer.

    If you have any questions, just ask.
    sudo rm -rf /

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

    Re: Help with fseek

    How would you recommend i correct this code to search and update.

  5. #4
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Help with fseek

    Before your fscanf line, use ftell() to remember the file offset at the beginning of the line. When you find the record, call fseek() using that file offset.
    sudo rm -rf /

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

    Re: Help with fseek

    That still not working,could you give me a code example or is its because im writing to a txt file and i heard that fseek and txt file don't get along very well

  7. #6
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Help with fseek

    Can you post your code? I thin I have an idea but it depends on your code.
    sudo rm -rf /

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

    Re: Help with fseek

    Hers it is or you want the entire program code

    Code:
    void updaterecord(void)
    {
         
       system("color 2");
       
       char choice4; 
       char Target[50];
       int Found=0;
       int tell=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)
                          {
                                                    
                             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,-80L,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);             
         
                           
    }

  9. #8
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Help with fseek

    Is every entry exactly 80 characters long? It doesn't look like it.
    sudo rm -rf /

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

    Re: Help with fseek

    i was just experimenting

  11. #10
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Help with fseek

    That's your problem. You completely ignored what I said.

    Code:
    void updaterecord(void)
    {
         
       system("color 2");
       
       char choice4; 
       char Target[50];
       int Found=0;
       int tell=0;
       long int offset = -1;
    
                  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)
                      {
                            offset = ftell(customer1);
                            
                            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)
                          { 
                            ...blah...
                            
                            
                            fseek(customer1,offset,SEEK_SET); 
                                
                                                
                            ...blah...
                           
                           }
                            else 
                            if(!Found)
                             printf("\n\n\t\t THERES NO SUCH RECORD");
                   }    
    
          fclose(customer1);                                   
    }
    sudo rm -rf /

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [C] Use a struct in a fseek()
    By stefanostefano in forum C and C++
    Replies: 3
    Last Post: 10-17-2010, 10:05 PM
  2. fread and fseek don't advance buffer
    By RobotGymnast in forum C and C++
    Replies: 2
    Last Post: 10-28-2008, 03:38 PM

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