嘿,我一直在試圖計數的單詞數在我的文本文件,加載了一堆話一個劊子手遊戲,從C,但我打磚壁。我使用的這段代碼應該是我正在使用這段代碼;計數採用C從文本的單詞數文件
FILE *infile;
FILE *infile;
char buffer[MAXWORD];
int iwant, nwords;
iwant = rand() %nwords;
// Open the file
infile = fopen("words.txt", "r");
// If the file cannot be opened
if (infile ==NULL) {
printf("The file can not be opened!\n");
exit(1);
}
// The Word count
while (fscanf(infile, "%s", buffer) == 1) {
++nwords;
}
printf("There are %i words. \n", nwords);
fclose(infile);
}
如果任何人有任何建議如何解決這個問題,我將非常感激。
文本文件每行1個字,用850個字。
應用緩衝區的建議,但字數仍出來的1606419282.
校正把
int nwords = 0;
工作過!非常感謝你!
不是答案 - 但請參閱http://www.leancrew.com/all-this/2011/12/more-shell-less-egg/ –
@MartinBeckett哈,今天在reddit上也看到了,呃? – Rooke
@Rooke昨天news.ycombinator.com! –