[英]Incompatible types in assignment of int to char
void trinti1()
{
int b,lines;
char ch[20];
FILE* file = fopen ("Kordinates.txt", "r");
while(!feof(file))
{
ch = fgetc(file);
if(ch == '\n')
{
lines++;
}
}
fclose(file);
}
Hello guys I am trying to count lines in file, but seems fgetc(file)
returns int
and it can't be converted to char. 大家好,我正在尝试计算文件中的行数,但是
fgetc(file)
似乎返回int
且无法将其转换为char。 Help me what I am doing wrong? 帮帮我,我做错了什么?
In your code ch
is not a char
, it is char[20]
- an array of 20 characters. 在您的代码中
ch
不是char
,而是char[20]
-20个字符的数组。 You cannot assign a result of fgetc
to it, because fgetc
returns an int
(which contains either a single char
, or an EOF
mark). 您不能为其分配
fgetc
的结果,因为fgetc
返回一个int
(包含单个char
或EOF
标记)。
Change the declaration of ch
to int ch
to fix this problem. 将
ch
的声明更改为int ch
可以解决此问题。 You can also drop the call to feof
because it happens at the wrong time anyway (you call it after read operations, not before read operations). 您也可以放弃对
feof
的调用,因为它总是在错误的时间发生(您在读取操作之后而不是在读取操作之前调用它)。
for (;;) {
int ch = fgetc(file);
if (ch == EOF) break;
if (ch == '\n') lines++;
}
void trinti1()
{
int b,lines=0;
int ch;
FILE* file = fopen ("Kordinates.txt", "r");
while(EOF!=(ch=fgetc(file)))
{
if(ch == '\n')
{
++lines;
}
}
fclose(file);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.