我的 while 循环中的错误代码,试图逐行读取文件。在 C
Error code on my while loop, trying to read line by line through a file. in C
我正在尝试从文件字典中获取输入,一次一行,我知道文件字典中的每一行只是一个单词。当我尝试编译此代码时收到错误代码,这里的错误是:
dictionary.c:66:36:错误:不同指针类型的比较('char ' 和 'int ()(FILE *)')
[-Werror,-Wcompare-distinct-pointer-types]
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;
feof
是库函数的标识符。请更改
while ( (fgets(word, 46, dic)) != feof )
至
while ( (fgets(word, 46, dic)) != NULL )
你得到它是因为你将 fgets
的结果与 函数 feof
的结果进行比较。您可能打算写 feof(fp)
或者 EOF
.
这也是错误的,因为 fgets
return 错误时为 NULL。
我正在尝试从文件字典中获取输入,一次一行,我知道文件字典中的每一行只是一个单词。当我尝试编译此代码时收到错误代码,这里的错误是:
dictionary.c:66:36:错误:不同指针类型的比较('char ' 和 'int ()(FILE *)') [-Werror,-Wcompare-distinct-pointer-types] 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;
feof
是库函数的标识符。请更改
while ( (fgets(word, 46, dic)) != feof )
至
while ( (fgets(word, 46, dic)) != NULL )
你得到它是因为你将 fgets
的结果与 函数 feof
的结果进行比较。您可能打算写 feof(fp)
或者 EOF
.
这也是错误的,因为 fgets
return 错误时为 NULL。