Tyler Gotto Tyler Gotto - 1 month ago 13
C Question

Error code on my while loop, trying to read line by line through a file. in C

I am trying to take input from the file dictionary, one line at a time, I know each line in the file dictionary is just one word. I am getting an error code when I try to compile this code and here the error is :

dictionary.c:66:36: error: comparison of distinct pointer types ('char ' and 'int ()(FILE *)')
[-Werror,-Wcompare-distinct-pointer-types]
while ( (fgets(word, 46, dic)) != feof )

I am very new to coding, and am not sure if this can be done this way, if I am trying to use the wrong method or if I am just coding this incorrectly. Thank you for nay help in advance.

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;

Answer

feof is the identifier of a library function. Please change

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

to

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