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.
An exapmle of the file is below.Code: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); }
1001 Jason.Roberts 4.Clover.Road .............
1002 ..................................
1003 ................................
I would want to get the 1003 and then begin from there.
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?
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.
Last edited by dargueta; 03-18-2010 at 01:34 PM. Reason: Clarification
sudo rm -rf /
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks