Linux: fgets读取文件总是多打印一行,解决方法是加if(feof(file1)) break;语句

修改前代码:

file1 = fopen("/home/michael/test_file_opfunc/test_file_opfunc.txt","r");
char ch[1024] = {0};
while (!feof(file1))
{   
    if(feof(file1)) break;
    printf("%s",ch);
           
}

修改后代码:

file1 = fopen("/home/michael/test_file_opfunc/test_file_opfunc.txt","r");
char ch[1024] = {0};
while (!feof(file1))
{   
    fgets(ch,100,file1);
    if(feof(file1)) break;
    printf("%s",ch);
}

结果比较: