Thanks for your help so far that i have achieved my goal.

My other problem is updating a particular record using fseek()

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)
                      {
                                    
                        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); 
                        
                        rewind(customer1);

                        fseek(customer1,sizeof(info),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);         
                                 
                        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,sizeof(info),SEEK_SET);
                        
                        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");                     
                       
                       }
                        else if(!Found)
                         printf("\n\n\t\t THERES NO SUCH RECORD");
                       }
           
          
             
         }
       
        
     
  
      fclose(customer1);             
     
                       
     }

Recommended Answers

All 5 Replies

why are you opening the same file twice (lines 11 and 22) ?

fseek() does not work well with text files.

>fseek() does not work well with text files.
More specifically, random offset access using fseek isn't guaranteed to be meaningful on text files. On a text file, you need to get some position indicators with ftell first, otherwise you're limited to boring stuff like seeking to the beginning of the file.

p.s. Someone else can describe the common gotchas in your code. I'm a little lazy right now.

Thanks for your help so far that i have achieved my goal.

My other problem is updating a particular record using fseek()

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)
                      {
                                    
                        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); 
                        
                        rewind(customer1);

                        fseek(customer1,sizeof(info),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);         
                                 
                        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,sizeof(info),SEEK_SET);
                        
                        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");                     
                       
                       }
                        else if(!Found)
                         printf("\n\n\t\t THERES NO SUCH RECORD");
                       }
           
          
             
         }
       
        
     
  
      fclose(customer1);             
     
                       
     }

How would it be implemented if i changed the the file form text to another

commented: Why are you quoting your own post? -2

If the customer structure does not contain any pointers then you could write the file out in binary mode so that fseek() to any given record position would be possible. Something like fwrite(fp, &Customer, 1, sizeof(struct customer)); Then read it back the same way but using fread() instead of fwrite().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.