我的while循环上的错误代码,尝试逐行读取文件。在C.

时间:2016-10-12 11:05:46

标签: c file while-loop hashtable

我试图从文件字典中获取输入,一次一行,我知道文件字典中的每一行只是一个单词。我尝试编译此代码时收到错误代码,错误是:

dictionary.c:66:36:错误:不同指针类型的比较(' char '' int()(FILE *)&#39 ;)       [-Werror,-Wcompare-不同指针类型]     while((fgets(word,46,dic))!= feof)

我对编码很新,并且我不确定这是否可以通过这种方式完成,如果我尝试使用错误的方法或者我只是错误地编码。感谢您提前提供帮助。

bool load(const char* dictionary)
{
    char word[46];
    unsigned long key;

    //remember file name
    FILE* dic = fopen(dictionary , "r");
    if (dic == NULL)
        {
            printf("Could not open file..\n");
            return false;
        }
    while ( (fgets(word, 46, dic)) != feof )
        {
            numberWords++;
            //Save new word
            node* newWord = malloc(sizeof(node));
            strcpy(newWord->dicWords, word);
            newWord->next = NULL;
            //Use Hash function on new word found
            key = hash(word);
            //Enter word into Hashtable
            if ( hashTable[key] == NULL )
                {
                    hashTable[key] = newWord;
                }
            else
                {
                    newWord->next = hashTable[key];
                    hashTable[key] = newWord;
                }
        }

    fclose(dic);

    return false;

2 个答案:

答案 0 :(得分:2)

feof是库函数的标识符。请更改

while ( (fgets(word, 46, dic)) != feof )

while ( (fgets(word, 46, dic)) != NULL )

答案 1 :(得分:0)

您得到它是因为您将fgets的结果与函数 feof进行了比较。您可能打算写feof(fp)EOF

这也是错误的,因为fgets在出错时返回NULL。