使用 strlen() 保存五个字母的字符串的问题

Issues using strlen() to save a five letter string

我制作了这个小程序来从一个包含所有英文单词的大文本文件中获取所有五个字母的单词。我想知道为什么我设置了 strlen(word)==6 而不是 5,即使在我 strtok "\n" 之后也是如此。我的朋友说我可能对 EOL 转换有疑问。我正在阅读它们,但我不太了解它们。

如果我遗漏了您可能需要帮助我的任何内容,请告诉我。也欢迎任何改进我的代码的建议。这是我的第一个 post 所以请多多包涵。谢谢。

#include <stdio.h>
#include <string.h>
#define SIZE 6

void manFile(char in_file[], char out_file[]);

int main(int argc, char **argv)
{
    manFile("words_alpha.txt", "fiveletterwords.txt");
}

void manFile(char in_file[], char out_file[])
{
    FILE*inFp= fopen(in_file, "r");
    FILE*outFp= fopen(out_file, "w");

    char word[20];

    if(!inFp)
    {
        printf("File not opened.");
    }

    else
    {
        while(fgets(word, sizeof(word), inFp))
        {
            strtok(word, "\n");
            
            if(strlen(word)==SIZE)
            {
                fprintf(outFp,"%s\n", word);
            }
        }

        printf("File saved...");

        fclose(inFp);
        fclose(outFp);
    }
}

如果你是 运行 在 windows-based 系统上,行以换行符 ('\n') 和 carriage-return ('\r') 终止,所以你可能想要:

strtok(word,"\n\r");