There are in my code functions that take a while to execute, like calculating the number of words in big files. I want to show to the user an output like this :
calculating word number ...
Execute the function then print : calculating word number ... OK
To do that, i have in my main :
int main(int argc, char * argv[])
{
int number_of_words;
FILE * dico = NULL;
dico = fopen(argv[1],"r+");
printf("calculating word number ...");
number_of_words = number(dico);
printf("OK\n");
return 3.14;
}
And the function that calculates the number of words is :
int number(FILE * dico)
{
int n=0;
char mot[20];
rewind(dico);
while (fgets(mot, 20, dico) != NULL)
{
n+=1;
}
return n;
}
After executing the function for a really big file for input, it appears that the output is not as attended. In fact, the waiting time is before the first printf("calculating word number ...");
and then when it is over all the printf
are done together.
Why does this happen ?