#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char** extract_words(char filename[], int size, int *total_words) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error open file\n");
exit(EXIT_FAILURE);
}
char buffer[255];
char word[32];
char **array = malloc(sizeof(char *) * size);
while (fgets(buffer, 255, fp) != NULL) {
char *p;
p = strtok(buffer, " ,.-\n");
while (p != NULL) {
array[*total_words] = _strdup(p);
(*total_words)++;
p = strtok(NULL, " ,.-\n");
}
}
fclose(fp);
return array;
}
int main()
{
char filename[32] = "d:\\data.txt";
int size = 1024, total_words = 0;
char **array = extract_words(filename, size, &total_words);
for (int i = 0; i < total_words; i++) {
printf("%s\n", array[i]);
free(array[i]);
}
free(array);
return 0;
}
/*
run:
C
is
a
general
purpose
programming
language
It
was
created
in
the
1970s
by
Dennis
Ritchie
and
remains
very
widely
used
and
influential
*/