Jump to content

Curious

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
hbk

hbk

    Learning Programmer

  • Members
  • PipPipPip
  • 71 posts
I want to store patient records by a incremental number so that no two records have the same id number. The problem is that when i close the program and have written the data to file, how do i retrieve the last id number to so that if the user which to add another customer, it starts from that point.


void addrecord(void)

{

     system("color 2");    

         

     customer1 = fopen("customer1.txt","a+");

    

     int num=0,x;

     int base=0;

     

     int regis=1000;

     

     printf("\n\n\t\t\t  PLEASE ENTER THE AMOUNT OF CUSTOMER YOU WANT TO ADD:");

     scanf("%d",&num);

     

     printf("\n\n\t\t\t  PLEASE ENTER THE CUSTOMER INFORMATION BELOW");    

        

       

    for(x=0;x<num;x++)

    {    

     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);

     

         

     system("cls");

     

     system("color 2");

     

     if (customer1==NULL)

            printf("\n\n\t\t\t  FILE WAS NOT FOUND ");

     else

         {

            regis++;                         

            

            fprintf(customer1,"%d %s %s %s %s %s %s %s\n",regis,info.Name,info.address,info.number,info.dob,info.treatment,info.allergies,info.app);

                              

         }

       

       } 

                   

     fclose(customer1);     

     

}               


An exapmle of the file is below.

1001 Jason.Roberts 4.Clover.Road .............
1002 ..................................
1003 ................................

I would want to get the 1003 and then begin from there.

#2
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
well, what you could do is scan all of the numbers and start from there, or what would be better is to either write a new file with just the last number in, or at the top of the file (customer1.txt)add the last number...
But I think you'll agree that the second option will be best?

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Just open the file for appending and the pointer will automatically go to the end of the file. See here for an explanation of how to do it. If you want to go to the last record (1003) instead of making a new one (1004) then just back up until you hit the second newline and you're set.

Edited by dargueta, 18 March 2010 - 12:34 PM.
Clarification

sudo rm -rf /