![](/img/trans.png)
[英]Trying to read a file line by line in C but the while loop never terminates
[英]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
在出错时返回NULL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.