我正在尝试编写代码,您可以在其中选择要打印的单词(如果它们与您选择的字母数量相同)。
如果您选择只打印包含 3 个字母的单词,它将忽略较长或较短的单词。
如果字母数与用户想要的相同,我不知道如何写一个单词。
这就是我到目前为止所拥有的。
do
{
ch = fgetc( fp );
if ( ch == ' ' || ch == EOF || ch == '\n' )
{
if(count == n) {
/* Here should be printed the word */
count = 0;
}
else count = 0;
}
else
{
++count;
}
} while ( ch != EOF );
最佳答案
您必须将字母存储在数组中,以便在需要时可以打印数组的内容。
char word[256];
do
{
ch = fgetc( fp );
if ( ch == ' ' || ch == EOF || ch == '\n' )
{
if(count == n) {
/* Here should be printed the word */
word[count] = '\0'; // terminate word
printf("%s\n", word); // print word
count = 0;
}
else
count = 0;
}
else
{
word[count] = (char)ch; // save the characters
++count;
}
} while ( ch != EOF );
关于c - 从文件中读取行,仅打印与用户选择的字母数量相同的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59144242/