XXX----Yep, its a Homework problem, but yea I'm stuck. Why doesn't this print out the elements of the array? Please help---XXX
Ok, we got the printing out part figured out. Thank you so much. Now the problem is the only the first character(s) before the space delimiter gets put into the array. I need all the words or characters to be set into the array.
int main(int argc, char** argv) {
int size = 0;
char **array = malloc(0); //malloc for dynamic memory since the input size is unknown
static const char filename[] = "input.txt";
FILE *file = fopen(filename, "r");
if (file != NULL) {
char line [ 128 ];
char delims[] = " ";
char *result = NULL;
while (fgets(line, sizeof line, file) != NULL) {
result = strtok(line, delims); //separate by space
size++;
array = realloc(array, size * sizeof (char*)); //declare array with unknown size
array[size - 1] = result;
}
fclose(file);
} else {
perror(filename);
}
return 0;
printf(array); //print array???
return (EXIT_SUCCESS);
}