Ok.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fid;
int * iarray;
int n=0;
fid = fopen("unsorted.txt", "r");
if (fid == NULL)
{
printf("Cannot open file\n");
exit(EXIT_FAILURE);
}
while( (fscanf(fid, "%d", &iarray[n]) != EOF ) )
{
iarray = (int *) malloc ( n * sizeof(int) );
/*fscanf(fid, "%d", &iarray[n]);*/
n++;
}
fclose(fid);
printf("%d\n", iarray[0]); prints the last number in the file
printf("%d\n", iarray[1]); prints 0 ???
printf("%d\n", iarray[2]); prints 0 ???
printf("%d\n", iarray[32]); prints 0 ???
printf("%d\n",n); 33 - there are 33 ints in the file
return(0);
}
What I take out of this is that the malloc part isnt working. My array will only take in 1 value, and keeps replacing this value with a new value until it exits the loop. Is this because you cannot update the array size after you have declared it once?