Greeting. I I crated a File that asks for name and age, I want to extract the number of ages that entered to the file, and that number print it on other file. I know that I can count based on how many names there are, or even the times I asked for information, but I need to count how many ages I got based on the number, in this case int, and print it on other file. Here is my code:
#define T 50
#define A 5
void main ()
{
   FILE *ap=NULL, *ap2=NULL;
   char cad[T];
   int age, x,cont=0;
   ap=fopen("Dat.txt", "w+"); //Open File
   if(ap==NULL)
   {  printf("Cant open the file");
      getch();
      exit(1);
   }
   ap2=fopen("Ages.txt","w"); //Open File
   if(ap2==NULL)
   {  printf("Cant open the file");
      getch();
      exit(1);
   }
   for(x=0;x<A;x++) //Gets the information
   {  printf("Name: ");
      gets(cad);
      printf("Age: ");
      scanf("%d",&age);
      fflush(stdin);
      fprintf(ap,"%-30s %d\n",cad,age);
   }
   rewind(ap);
   fgets(cad,T,ap);
   while(!feof(ap))     //Start counting the ages
   {  fscanf(ap,"%d",&age);
      ++cont;
   }
   fprintf(ap2,"%d", cont);
   fclose(ap); fclose(ap2); //Close both Files
It works fine creating the file "Dat.txt" with all the information if I comment the last 6 lines of code (except fclose), but it seems to enter into a Loop, because it doesn't do anything when I finish entering the information.
 
    