Part of your problem has to o with the while condition. Checking here for eof does n always work the way you might thing. line 14 will reach eof and print to your output file before the while condition is reached.
When eof is reached the while loop will fail and the output file will be correct.
You will also have to change the size of the array in line 10 to handle the total number of words in the input file. And you will have to define "index" and initialize it to zero before you use it.
I was planning to run a function that sorts the string array but i can not use the example you provided because it will just take all the words at once