What is the goal of the program? To save all the words into a two-dimensional array? Another way is to make an array of strings. For example let's allocate some memory called dict to store the total list of words, and then allocate an array of pointers called list, each member of which points to the appropriate spot in dict.
Code:
#define DICTMAX 1000000
#define LISTMAX 10000
char buf[30];
char dict[DICTMAX] = "";
char *dictend = &dict[DICTMAX];
char *list
[LISTMAX] = {};
char **listend = &list
[LISTMAX];
void listprint(char **list);
int main()
{
FILE *fp = fopen("in.txt", "r");
char *d = dict;
char **l = list;
for (; fscanf(fp, "%29s", buf)==1; d += strlen(buf)+1, l++)
{
strncpy(d, buf, dictend-d);
*l = d;
if (dictend-d < strlen(buf)+1)
break; // no more room in words
if (listend-l == 0)
break; // no more room in list
}
fclose(fp);
int numwords = l-list;
printf("Number of words: %d\n", numwords);
listprint(list);
return 0;
}
void listprint(char **list)
{
for (char **l = list; listend-l != 0; l++)
printf("%s\n", *l);
}
Notes:
1. words are defined as a sequence of non-whitespace characters
2. no word is longer than 29 characters; this number can be increased as needed without increasing the size of dict or list.
3. the total number of words is not greater than LISTMAX
4. the total number of word characters is not greater than DICTMAX+k, where k is the number of words encountered so far