为什么在这段代码中,当我从文件打印字符时打印空字符以及为什么在两个文件中找到更多空值

问题描述 投票:0回答:1
bool isIdentical(FILE *file1, FILE *file2) //this fun tell me files is identical or no
{
    // this must have char1 in file1?  
    char c1 = fgetc(file1);

    // this must have char2 in file2? 
    char c2 = fgetc(file2);

    while (c1 != EOF && c2 != EOF)  //check if files reached to end 
    {
        c1 = fgetc(file1);
        c2 = fgetc(file2);
        if (c1 != c2)
            return 0;

        // I expect to print each char in to files but it actual print NULL  
        printf("%c %c\n", c1, c2);
    }

    // this to check if two files reached to end without returning 0
    if (c1 == EOF && c2 == EOF)
        return 1;
    else
        return 0; // then the files don't have the same length hence are not identical
}
c file printing
1个回答
0
投票

您必须使用类型

c1
而不是
c2
来定义
int
char
,才能可靠地测试
EOF
fgetc()
有257个不同的返回值,
char
只能容纳256个。

© www.soinside.com 2019 - 2024. All rights reserved.