如果字符在 IF 语句中,fgetc(file Name) 是否会拉出该字符?

Will fgetc(fileName) pull a character if it is within an IF statment?

这是我的 if 语句:

if (fgetc(fileName) != EOF) {
}

我知道,如果我 运行 fgetc() 不在 if 语句中,它将删除一个字符,我将不得不执行 ungetc 到 return 它。当 fgetcif 语句中时会发生这种情况吗?

是的,因为您没有存储读取的字节,所以丢失了它。改用这个:

int c;
if ((c = fgetc(fp)) != EOF) {
    ungetc(c, fp);   /* put the byte back into the input stream */
    /* not at end of file, keep reading */
    ...
} else {
    /* at end of file: no byte was read, nothing to put back */
}

还请注意,您应该将 FILE* 传递给 fgetc,而不是文件名,并且 ungetc 不会 return 字符,而是将其放回输入流,以便它可以被下一个 fgetc()fgets() 读取...在再次从流中读取之前,您可能一次 ungetc 不能超过一个字节。