First up, the segment:
int word_count()
{
int wordcount(FILE *infile);
should be:
int word_count(FILE *infile) {
Secondly, fgets()
takes three arguments, the buffer address, maximum size and file handle.
Third, if fgets()
gets something, it returns a pointer to the buffer, not a character. If you're wanting to handle characters, you should be looking into fgetc
instead.
Fourth, detecting the
character is a good way to count lines but wot words. And you'd probably want to detect a final line without a newline on it as another line anyway (depending on your needs).
If you want to count words, you need to detect transitions between words and non-words.
Fifth, you should either move the word counting function to above main
or provide a prototype for it above main
. As it is now, it's not declared so you don't have type safety.
And, finally, main()
should be declared to return an int
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…