C 中的一个简单文件处理代码卡在 do/while 循环

A simple file handling code at C stuck at do/while loop

int main(void) {
    int status = EXIT_SUCCESS;
    FILE *in;

    struct sigrecord {
        int signum;
        char signame[10];
        char sigdesc[100];
    } sigrec;
    if ((in = fopen("signals.txt", "r")) == NULL) {
        fputs("Cannot open signals.txt file\n", stderr);
        return EXIT_FAILURE;
    }
    do {
        int n = fscanf(in, "%d%9s%*[ \t]%99[^\n]", &sigrec.signum,
                       sigrec.signame, sigrec.sigdesc);
        if (n == 3) {
            printf("Signal\n number = %d\n name = %s\n description = %s\n\n",
                   sigrec.signum, sigrec.signame, sigrec.sigdesc);
        } else if (n != EOF) {
            fputs("Failed to match signum, signame or sigdesc\n", stderr);
            status = EXIT_FAILURE;
            break;
        }
    } while (1);
    if (fclose(in) == EOF) {
        fputs("Failed to close file\n", stderr);
        status = EXIT_FAILURE;
    }
    return status;
}

输出很好,但是 while(1) 循环没有结束 do{} 循环,我的程序在完成它的目的后卡住了,完美地打印输出。感谢您的帮助。

如果 scanf 尽管有原因但没有成功,您需要 break

} 
else {
    if (n != EOF) {
        fputs("Failed to match signum, signame or sigdesc\n", stderr);
        status = EXIT_FAILURE;
    }

    break;
}