用C Wordcounting程序 [英] Wordcounting program in C

查看:339
本文介绍了用C Wordcounting程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试写统计一个文本文件中的单词数的程序。

So I am trying to write a program that counts the number of words in a text file.

main(void){
    int wordcount = 0;
    FILE *infile = fopen("Text.txt", "r");
    wordcount += word_count(infile);
    printf("%d", wordcount);
    return 0;

    }

int word_count()
{
    int wordcount(FILE *infile);
    int count;
    char it;
    while ((it  = fgets(infile)!=EOF))
    {
        if (it =='\n')
        {
            count++
        }
    }
    return count;
}

但是当我运行它,我得到了这些错误:

But when I run it, I got these errors:

infile undeclared identifier
fgets too few aruguments for function call

我在做什么错了?

what am I doing wrong?

推荐答案

首先登场,段:

int word_count()
{
    int wordcount(FILE *infile);

应该是:

int word_count(FILE *infile) {

第二,与fgets()需要的的参数,缓冲区地址,最大大小和文件句柄。

Secondly, fgets() takes three arguments, the buffer address, maximum size and file handle.

第三,如果与fgets()得到的东西,它返回一个指向缓冲区,而不是一个字符。如果你想处理的字符,你应该寻找到龟etc 代替。

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.

四,检测 \\ n 字符来算的的WOT,但话的好方法。而且你可能要检测一个最后一行没有它的换行符为另一行呢(根据您的需要)。

Fourth, detecting the \n 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.

最后,的main()应声明为返回一个 INT

And, finally, main() should be declared to return an int.

这篇关于用C Wordcounting程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆