while ( fscanf ( fp, "%d", &value) == 1)
{
o++;
}
now you're at EOF
if ( o > t_size)
{
printf ( "too many integers\n");
getchar();
return 1;
}
if ( o < t_size)
{
printf ( "not enough integers\n");
getchar();
return 1;
}
printf("Create a size of %d array\n", t_size);
int* my_array = NULL;
my_array = malloc(t_size*sizeof(*my_array));
if (my_array==NULL) {
printf("Error allocating memory!\n");
return 1;
getchar();
}
These are simple static checks, they work just fine.
The subsequent fscanf where does it pick the data from? AFTER EOF. Bad.
int i =0;
for ( i = 0; i < t_size; i++ )
{
fscanf(fp, "%d",&my_array[i]);
}
}
If you add the sequent lines before the above cycle
fclose(fp);
fp = fopen ("xxx.txt", "r");
fscanf(fp, "%*d");
it should work. That is the first way.
Also note that making two trivial integer checks in a loop is not slower than going TWICE through the file.