Friday 31 March 2017

c - Why does this program behave like `tail -f`

int main(int argc, char *argv[])
{
int c = EOF;
FILE *fp = fopen("./temp.txt", "r");
assert(fp!=NULL);
while (1) {
c = fgetc(fp);
if (EOF != c) {
putchar(c);

}
}

return 0;
}


temp.txt is a slowly increasing log file, so this program can read the EOF. After it first encounters EOF, I thought it should stop getting new added data of temp.txt, while it just acts like tail -f temp.txt and continues printing new lines of the file.



Yes, I know there is an infinite loop. The problem is that I thought,

when fgetc first encounters EOF, it should do some recording in the struct fp,
and the next callings of fgetc should check this and return EOF immediately.
Why it continues to read the data on the disks, didn't it reach the end-of-file?
Is this the expected behavior?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...