Jump to content

Just an idea

- - - - -

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

#1
hbk

hbk

    Learning Programmer

  • Members
  • PipPipPip
  • 71 posts
I want to scan a number from a file and use it as means as a checkpoint to another file. The truth is i am writing data to a file based on id numbers and i want when the compiler is closed and reopened the data can continue be entered based on the last id number entered.

I am using two files to carry out such task, one file hold all the information and the other i want just to store the last id number entered by the user.

So when the user runs the program another time it shuld read data from the second file and use it as a chechpoint.

void addrecord(void)

{

     system("color 2"); 

     

     FILE *cust;   

         

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

     cust=fopen("temp.txt","a+");

    

     int num=0,x;

     int base=0;

     

     int regis=1000;

     int regis2=1000;

          

     fscanf(cust,"%d",&info.regis2);

     

     regis=info.regis2;      

             

     

     printf("\n\n\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=1;x<=num;x++)

    {                        

                         

     printf("\n\n\t\t\t       ENTER PATIENTS NUMBER %d DETAILS",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);

            

                               

         }

         

         if(cust==NULL)

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

       else

       {    

               regis2++;                          

                if (x==num)

                   fprintf(cust,"%d",regis2);

                   }      

       

        } 

       

       

       

       

       

     fclose(cust);              

     fclose(customer1);     

     

}               


File 1
1001 .................................
1002.................................

File 2
1002

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Wait...so what's the question?
sudo rm -rf /

#3
hbk

hbk

    Learning Programmer

  • Members
  • PipPipPip
  • 71 posts
how do i achieve the task.

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Well, I'm assuming you know how to use fscanf, so you can just read the record number from there, and the open your file in append mode and the write pointer will go straight to the end.
FILE *last, *records;
last = fopen("last.txt","r+");
records = fopen("records.txt","a+");

int lastrecord;
fscanf(last,"%d",&lastrecord);

/* now you have the last record number,
and can write directly into the file to make
a new record */

fclose(last);
fclose(records);

sudo rm -rf /