C语言,ege图形库,这段代码,有时候最后一行会打印两遍,有时候不会,这是什么原因?有没有避免的方法?

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
#include <graphics.h>
#include <stdio.h>

int main()
{
    initgraph(800, 640, INIT_RENDERMANUAL);
    setbkcolor(WHITE);    
    
    //打开文件
    FILE* fp = fopen("F:\\c语言课设\\bookslist.txt","r");
    if (fp == NULL) {
        xyprintf(0, 0, "打开文件失败");
        getch();
        return -1;
    }

    setcolor(BLACK);

    int titleHeight = 40;
    setfont(40, 0, "楷体");
    xyprintf((800 - textwidth("书库")) / 2, 0, "书库");

    int fontHeight = 20;
    setfont(fontHeight, 0, "楷体");
    int x = 40,y = titleHeight + 20;

    char buff[1024];
    
    while (!feof(fp)) {
        fgets(buff, 1024, fp);
        xyprintf(x, y, buff);
        y += fontHeight;
        if (y + fontHeight > 600) {
            y = titleHeight + 20;
            if ((x += 400) > 800)
                break;
        }
    }

    fclose(fp);
    
    getch();

    closegraph();

    return 0;
}

运行结果及报错内容

这段代码,有时候最后一行会打印两遍,有时候不会,这是什么原因?有没有避免的方法?

我的解答思路和尝试过的方法
我想要达到的结果

最后一行代码只输出一遍

char * p = fgets(buff, 1024, fp);
if(p != NULL)
xyprintf(x, y, buff);
这样试试

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632