This works here for the first part of the code..
How's it looks guys??
*note* I put it all in a function to create more organization for later.
Ok cool ooga I see what you did, with that. Not as familiar with that way right now. But definitely seems to make it easier on the coder in not having to write as much.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **getWords(char *wordsFile);
int main()
{
char *wordsFile = "words.txt";
char **words;
words = getWords( wordsFile );
getchar();
return 0;
}
char **getWords(char *wordsFile)
{
FILE *fp;
char buffer[20];
char **words, **temp;
int i=0;
int n = 100;
words = malloc(100 * sizeof(char *) );
if ( (fp = fopen("words.txt", "r" )) == NULL )
{
printf("Couldn't open file\n");
exit(1);
}
while( fgets(buffer, sizeof(buffer), fp))
{
strtok(buffer, "\n");
words[i] = malloc(strlen(buffer)+1);
strcpy(words[i], buffer);
printf("%d: %s\n", i+1, words[i]);
if ( (i+1) == n )
{
n = n*2;
temp= realloc(words, n*sizeof(char*));
if(temp != NULL)
words = temp;
else
{
printf("unable to reallocate\n");
exit(1);
}
printf("newline here\n");
}
i++;
}
fclose(fp);
return words;
}