Closed Thread
Results 1 to 3 of 3

Thread: Curious

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

    Curious

    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.

    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);     
         
    }
    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    lintwurm's Avatar
    lintwurm is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Pretoria
    Posts
    81
    Rep Power
    0

    Re: Curious

    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?

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

    Re: Curious

    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 /

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Curious about the following js/css video
    By hackmyballs in forum Java Help
    Replies: 2
    Last Post: 12-09-2010, 06:40 PM
  2. Just curious.....
    By zprogrammer420 in forum Java Help
    Replies: 4
    Last Post: 04-29-2010, 08:19 PM
  3. Curious...
    By ImTheMessenger in forum Software Development Tools
    Replies: 3
    Last Post: 04-28-2010, 01:03 AM
  4. Just Curious
    By Whitey in forum PHP Development
    Replies: 6
    Last Post: 02-11-2010, 09:33 AM
  5. Curious.
    By rescueme in forum C and C++
    Replies: 15
    Last Post: 02-11-2009, 04:19 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