tried to look on forums but could not reslove.
试图在论坛上看,但无法重新开始。
I'm trying to read from text. Text is: "To b"
我正试图从文本中读取。文字是:“到b”
But while using fgetc(), EOF is not reached, and at the end I get '\n' and then infinity 'y' samples.
但是在使用fgetc()时,没有达到EOF,最后我得到'\ n'然后是无限'y'样本。
Here's my code:
这是我的代码:
Node* getBinTree(FILE *fsrc){
Node* root=NULL;
unsigned char tmp=NULL;
while ((tmp=fgetc(fsrc))!=EOF)
globalArray[tmp]++;
return root;
}
thanks a lot
非常感谢
3 个解决方案
#1
The trouble you have is related to what fgetc
returns. The return type is int
but you are storing it into an unsigned char
.
你遇到的麻烦与fgetc的回归有关。返回类型是int,但是您将它存储到unsigned char中。
You must either change this to be an int
or, as an alternative, use feof
to check for an end of file condition.
您必须将此更改为int或作为替代方法,使用feof检查文件结束条件。
#2
Use int tmp
. EOF can not be stored in a char because it is not a char.
使用int tmp。 EOF不能存储在char中,因为它不是char。
#3
fgetc() returns a signed integer, but your program stores the result in an unsigned char. When converting from signed to unsigned types, a negative number (EOF is often defined to be -1) becomes positive (decimal 256, in this case), so if EOF is negative, the comparison of the return value with EOF will always return false. To fix the code, change the declaration of "tmp" from an "unsigned char" to an "int".
fgetc()返回一个有符号整数,但是你的程序将结果存储在unsigned char中。当从有符号类型转换为无符号类型时,负数(EOF通常定义为-1)变为正数(在这种情况下为十进制256),因此如果EOF为负数,则返回值与EOF的比较将始终返回false 。要修复代码,请将“tmp”的声明从“unsigned char”更改为“int”。
#1
The trouble you have is related to what fgetc
returns. The return type is int
but you are storing it into an unsigned char
.
你遇到的麻烦与fgetc的回归有关。返回类型是int,但是您将它存储到unsigned char中。
You must either change this to be an int
or, as an alternative, use feof
to check for an end of file condition.
您必须将此更改为int或作为替代方法,使用feof检查文件结束条件。
#2
Use int tmp
. EOF can not be stored in a char because it is not a char.
使用int tmp。 EOF不能存储在char中,因为它不是char。
#3
fgetc() returns a signed integer, but your program stores the result in an unsigned char. When converting from signed to unsigned types, a negative number (EOF is often defined to be -1) becomes positive (decimal 256, in this case), so if EOF is negative, the comparison of the return value with EOF will always return false. To fix the code, change the declaration of "tmp" from an "unsigned char" to an "int".
fgetc()返回一个有符号整数,但是你的程序将结果存储在unsigned char中。当从有符号类型转换为无符号类型时,负数(EOF通常定义为-1)变为正数(在这种情况下为十进制256),因此如果EOF为负数,则返回值与EOF的比较将始终返回false 。要修复代码,请将“tmp”的声明从“unsigned char”更改为“int”。