Well here's a working example:
Code:
int main()
{
char choice[15];
int num = 0;
FILE *fp;
char *file = "Data.dat";
do{
fp = opens(file,"wb","The File For Creation",1);
fclose(fp);
fp = opens(file,"ab+","The File For Appending",1);
clrscr();
carr(7);
printf("Please Enter The First Number \nOf An Array Of Integers (\"Q\" Exits ) : \n\n");
fgets(choice,15,stdin);
do{
num = atoi(choice);
fwrite(&num,sizeof(int),1,fp);
num = 0;
clrscr();
carr(7);
printf("Input The Next Integer (\"Enter\" To Total ) : \n\n\n");
fgets(choice,22,stdin);
}while(atoi(choice));
int num = ftell(fp) / sizeof(int);
rewind(fp);
int array[num];
int i, total = 0, hi = 0, lo = 0, flag = 0;
for(i=0; i<num; i++)
{
fread(&array[i],sizeof(int), num ,fp);
total += array[i];
if(flag == 0){ lo = array[i];}
if(array[i] < lo){lo=array[i];}
if(array[i] > hi){hi=array[i];}
printf("Array[%i]: %i \n",i+1, array[i]);
getch();
flag++;
}
fclose(fp);
int avg = total / num ;
printf("Number of integers in array: %i .\n\n", num);
printf(" Total: %5i. Average: %5i \n\n",total,avg);
printf("Highest: %5i. Lowest: %5i \n",hi,lo);
getch();
clrscr();
carr(7);
printf("Do Another? (\"Q\" Exits) : \n\n");
fgets(choice,15,stdin);
}while(!strstr(choice,"Q") && !strstr(choice,"q"));
return 0;
}
In my view, there are times when it is better to malloc and times when the simplicity of exploiting the natural virtues of a language (in this case, scope) is better.
If nothing else, it is a good example of simplifying a simple program!
Had this been a large, complex problem, malloc ( and probably linked-lists) would probably have been more suitable, I suppose.