how can I read a data file to an array:
Have tried using fread() but doesnt work
It could be that your file isn't the same size as your array...
The done variable should equal 1 after the fread if successful, because it is reading 1 array 'block'.
done = fread(fp, sizeof(array),1,input);
Of course, this will only work if you have outputted the whole array to the file using fwrite.
> if ((input=fopen("data.txt","r"))==NULL)
When you load data.txt file into your editor, do you see
- a mess of hexadecimal bytes and lots of \0 ?
In which case fread is appropriate.
- a nice easy to read file containing say
1 2 3 4
In which case you should be using fgets + sscanf (or fgets + strtol).
got it working
worked it out;
file = fopen("data.txt","r");
perror("Error opening file");
array=strtok(string, " \n\t");
array[i]=strtok(NULL, " \n\t");
Since you're breaking out of the loop at the first occurrence of NULL, you might as well have stopped doing the strtok() at that point too... saving a lot of work. Instead of blindly going for a full 50000.