Я пишу код для извлечения всех слов из файла .txt, но у меня проблемы. Я хочу разрешить только буквы и апострофы, отсюда и разделители, которые я выбрал. Вот мой код:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
const char *separators =
"\n\r !\"#$%&()*+,-./0123456789:;<=>?@[\\]^_`{|}~";
size_t len = 1000;
char *word2 = (char *)malloc(len);
FILE *file2 = fopen("words.txt", "r");
if (file2 == 0)
{
fprintf(stderr, "Failed to open second file for reading\n");
exit(EXIT_FAILURE);
}
while (fgets(word2, sizeof(word2), file2))
{
char *token = (char*)strtok(word2, separators);
while (token != NULL)
{
printf("%s", token);
printf("\n");
token = strtok(NULL, separators);
}
}
return 0;
}
А вот что есть в словах.txt:
This is a sentence in the file
Мой вывод в конечном итоге будет
This
is
a
sent
ence
in
the
fi
le
Кто-нибудь знает, почему это так?