我已經詳細瞭解了所有相關的問題,以量化文本文件中某個單詞的出現,並且我仍然無法獲得該單詞的執行和計數。我沒有得到一個錯誤,但返回的輸出是:C字發生
打開的文件 字符數= 0 4195872
恰好有6個字在「file.txt的」「通信」,我需要的只是計數器返回值6.任何幫助將非常感激。
#include <stdio.h>
#include <stdlib.h>
int main(){
int counter;
int ch;
int str;
FILE *input;
input = fopen ("file.txt", "r");
if (input == NULL)
{
printf("File failed to open \n");
}
else
{
printf("File opened \n");
do {
str = fgetc(input);
// if ((char*)str == keyword) counter++;
if (str == ch)counter++;
} while (str != EOF);
printf("character count = %i %i\n", counter, ch);
fclose(input);
}
return 0;
}
'ch'永遠不會被初始化。 –
和'counter'。 – BLUEPIXY
在你的行'if(str == ch)counter ++;'你只是數字而不是單詞。你必須通過字符匹配字符來計算字數。如果我不清楚,請告訴我。 – jml