简体   繁体   English

int分配给char的类型不兼容

[英]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 (包含单个charEOF标记)。

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.

相关问题 将char **分配给char *中的类型不兼容 - Incompatible types in assignment char ** to char * 将float分配给char [3]中的类型不兼容 - incompatible types in assignment of `float' to `char[3]' 将char *分配给char [200]时类型不兼容 - incompatible types in assignment of char * to char[200] 将“char [10]”分配给“char [50]”时的类型不兼容 - incompatible types in assignment of 'char[10]' to 'char [50]' 两个字符数组之间的分配中的类型不兼容 - Incompatible Types in Assignment Between Two Char Arrays 当我运行我的 C 程序时,我的编译器返回“从不兼容的指针类型‘char *’[-Wincompatible-pointer-types] 分配给‘int *’” - My compiler returned "assignment to 'int *' from incompatible pointer type 'char *' [-Wincompatible-pointer-types]" when I ran my C program 当打开文件并将其分配给char时,出现“ Incompatible Types”错误 - When open the files and assignment to char ,Incompatible Types error 在C中将struct char数组分配给char数组给出了“分配中的类型不兼容”错误 - Assignment of struct char array to char array in C is giving an “incompatible types in assignment” error C编译器:“类型'char *'和'int'不是赋值兼容的” - 但是没有int? - C compiler: “Types 'char *' and 'int' are not assignment-compatible” - but there's no int? C:分配中的类型不兼容 - C: incompatible types in assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM