
December 10th, 2012, 09:54 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 2
Time spent in forums: 49 m 28 sec
Reputation Power: 0
|
|
|
Undefined ungetc behavior
hi!
i'm new at c so i'm trying to understnad something. here it is:
main.c:
Code:
#include <stdio.h>
int main(void)
{
FILE *fp;
fp = fopen("test.txt", "r+");
putchar(fgetc(fp));
putchar(fgetc(fp));
putchar(fgetc(fp));
//putchar(fgetc(fp));
ungetc('t', fp);
ungetc('U', fp);
ungetc('P', fp);
putchar('\n');
putchar(fgetc(fp));
putchar(fgetc(fp));
putchar(fgetc(fp));
}
test.txt:
outputs as:
i'm using fgetc to make sure the file position gets increment once evry call  .
but when i uncomment the 4th putchar(fgetc(fp)), the output become like this:
it's like after EOF was reached, we can effectiively call ungetc only once. that's it... i thoght you can do it at least 3 times, like what happened before we uncommented the putchar(fgetc(fp)).
why???
|