比较C冗余代码中的两个文件[暂停]

时间:2021-10-06 16:07:10

Below I have a code to compare two files and check if they are identical or not. I did not write it, but am trying to understand the logic of it. I get the general idea, but specifically am confused about if some of it is redundant or is this all vital so that the program runs?

下面我有一个代码来比较两个文件,并检查它们是否相同。我没有写它,但我试图理解它的逻辑。我得到了一般的想法,但特别是我很困惑,如果其中一些是多余的,或者这一切是否至关重要,以便程序运行?

There are notes where my confusion lies!

有笔记我的困惑所在!

Thanks in advance for any type of help.

提前感谢任何类型的帮助。

#include <stdio.h>

int main ()
{
    int ch1, ch2; 

    FILE *f1 = fopen("one.txt", "r");
    if(f1 == NULL)
    {
        printf("Error: file cannot be opened\n");
    }
    FILE *f2 = fopen("two.txt", "r");
    if(f2 == NULL)
    {
        printf("Error: file cannot be opened\n");
    }

    ch1 = getc(f1); //does this need to be here? it is already in the loop
    ch2 = getc(f2); 

    while((ch1 != EOF) && (ch2 != EOF) && (ch1 == ch2)) 
    {
        ch1 = getc(f1);
        ch2 = getc(f2);
    }

    if(ch1 == ch2)
        printf("Files are identical\n");
    else if(ch1 != ch2)
        printf("Files are not identical\n");

    fclose(f1);
    fclose(f2);
}

1 个解决方案

#1


5  

What you really want is a do-while loop. This is equivalent to what you have but it's more clear:

你真正想要的是一个do-while循环。这相当于你所拥有的,但更清楚:

do {
    ch1 = getc(f1);
    ch2 = getc(f2);
} while ((ch1 != EOF) && (ch2 != EOF) && (ch1 == ch2));

This allows you to specify the calls to getc() only once per variable.

这允许您为每个变量仅指定对getc()的调用一次。


Since main() returns int, you should make sure to return 1 to indicate an error or 0 for success. See What should main() return in C and C++? for more information.

由于main()返回int,因此应确保返回1表示错误,或者返回0表示成功。请参阅C和C ++中main()返回的内容?欲获得更多信息。

Also, your error conditions should be improved to prevent using file handles when they are NULL. E.g.,

此外,应该改进您的错误条件,以防止在它们为NULL时使用文件句柄。例如。,

if(f1 == NULL)
{
    printf("Error: file cannot be opened\n");
}

Should be:

if(f1 == NULL)
{
    printf("Error: file cannot be opened\n");
    return 1;
}

And:

if(f2 == NULL)
{
    printf("Error: file cannot be opened\n");
}

Should be:

if(f2 == NULL)
{
    printf("Error: file cannot be opened\n");
    fclose(f1); // Close the first file which was successfully opened.
    return 1;
}

And at the end, indicate success:

最后,表明成功:

fclose(f1);
fclose(f2);
return 0;

#1


5  

What you really want is a do-while loop. This is equivalent to what you have but it's more clear:

你真正想要的是一个do-while循环。这相当于你所拥有的,但更清楚:

do {
    ch1 = getc(f1);
    ch2 = getc(f2);
} while ((ch1 != EOF) && (ch2 != EOF) && (ch1 == ch2));

This allows you to specify the calls to getc() only once per variable.

这允许您为每个变量仅指定对getc()的调用一次。


Since main() returns int, you should make sure to return 1 to indicate an error or 0 for success. See What should main() return in C and C++? for more information.

由于main()返回int,因此应确保返回1表示错误,或者返回0表示成功。请参阅C和C ++中main()返回的内容?欲获得更多信息。

Also, your error conditions should be improved to prevent using file handles when they are NULL. E.g.,

此外,应该改进您的错误条件,以防止在它们为NULL时使用文件句柄。例如。,

if(f1 == NULL)
{
    printf("Error: file cannot be opened\n");
}

Should be:

if(f1 == NULL)
{
    printf("Error: file cannot be opened\n");
    return 1;
}

And:

if(f2 == NULL)
{
    printf("Error: file cannot be opened\n");
}

Should be:

if(f2 == NULL)
{
    printf("Error: file cannot be opened\n");
    fclose(f1); // Close the first file which was successfully opened.
    return 1;
}

And at the end, indicate success:

最后,表明成功:

fclose(f1);
fclose(f2);
return 0;