0

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.

  • I don't understand your problem, but I have the feeling it has to do with [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/2371524) -- apart from that, [**never use `gets()`!**](https://stackoverflow.com/q/1694036/2371524) –  May 02 '18 at 07:08
  • I'm supposed to create a single file, in this case "Dat.txt" which will have many names and many ages that I want, since the code works creating that file. I need to read it again (That's why I use w+), and based of the number of ages that I get while I'm reading, I'll create a new file called "Ages.txt" which will have the number of ages that I counted. – Alberto Zavala May 02 '18 at 07:22
  • Had you tested the return value of `fscanf`, it would be evident: `fscanf(ap,"%d",&age);` returns 0 because it is trying to read a name and cannot decode it as an int. And `fflush(stdin)` in non standard and is only a Microsoft extension. It will not clean the input stream with gcc or clang... – Serge Ballesta May 02 '18 at 07:57
  • I used 'fflush(stdin)' because when I finish to write the age, automatically ask me for the next age in 'for' loop, it seems like '\n' is getting inside to the string. and it kept me confuse about 'fscanf' because I want to print the age separately when I'm reading the file but I don't know how. I tried to print the string with 'puts(cad)' and I noticed that the string came with the age... So not sure how to do it – Alberto Zavala May 02 '18 at 08:16

1 Answers1

0

Th problem is in line fscanf(ap,"%d",&age); ofwhile loop. This is because You have written a string fist and you are expecting to read an int.

Replace it with following code snippet and it will solve your issue:

...
rewind(ap);
fgets(cad,T,ap);
while(!feof(ap))     //Start counting the ages
{
   //Move pointer to 30, Since while writing to file width is set to 30(%-30s) 
   age = atoi(cad+30); 
   printf("%d\n",age);
   ++cont;
   fgets(cad,T, ap);
}
fprintf(ap2,"%d", cont);
...
cse
  • 4,066
  • 2
  • 20
  • 37
  • Thanks, that helped me with the loop, but I'm still confused, if I use _puts(cad);_ after _fscanf_ inside While loop, I see that prints the name with the age, and if I try to print that number separately , using _printf("Age: %d\n",age)_ it prints the last age that was registered at the begging. Is there any way to print the age separately from the file? – Alberto Zavala May 02 '18 at 08:04