A
av
Why is so danger to allow ungetc(EOF, pfile); (for close the imput
stream) ?
stream) ?
av said:Why is so danger to allow ungetc(EOF, pfile); (for close the imput
stream) ?
av said:Why is so danger to allow ungetc(EOF, pfile); (for close the imput
stream) ?
EOF is not a character in any stream. Trying to 'push' it back onto the
stream doesn't make sense.
Given..
int ch;
ch = getc(pfile);
if (ch == EOF)
{
/* Assuming no error, getting here means the the previous call to
getc(pfile) was the last character in the stream.
*/
}
ungetc pushes characters back onto the stream, EOF isn't a character.
We already have fclose for closing a stream.
close in the way "pfile->flag|= _EOF;"
the above seems wrong
noone of you know if it is right "ungetc(EOF, pfile);"?
and for what it is intended to do?
EOF is not a character in any stream. Trying to 'push' it back onto the
stream doesn't make sense.
Given..
int ch;
ch = getc(pfile);
if (ch == EOF)
{
/* Assuming no error, getting here means the the previous call to
getc(pfile) was the last character in the stream.
*/
}
int ch1, ch2, ch3;
ch1=getc(stream); // ch1='2'
ch2=getc(stream); // ch2="."
ch3=getc(stream); // ch3=EOF
ungetc(EOF);
ungetc(ch2);
int ch1, ch2, ch3;
ch1=getc(stream); // ch1='2'
ch2=getc(stream); // ch2="."
ch3=getc(stream); // ch3=EOF
ungetc(EOF);
ungetc(ch2);
You can't unget more than one char.
av said:if i do it, it means i can do it
ungetc set a flag in FILE if fail
Congratulations, that's simultaneously tautological and false.
Oh? What flag would that be?
C99 7.19.7.11p3:
One character of pushback is guaranteed. If the ungetc function is
called too many times on the same stream without an intervening
read or file positioning operation on that stream, the operation
may fail.
A failing ungetc() is indicated by a return value of EOF.
av said:av writes: [...]ungetc set a flag in FILE if fail
Oh? What flag would that be?
C99 7.19.7.11p3:
One character of pushback is guaranteed. If the ungetc function is
called too many times on the same stream without an intervening
read or file positioning operation on that stream, the operation
may fail.
A failing ungetc() is indicated by a return value of EOF.
yes thank you, it seems i have forget this case and have correct my
code
now in my version of ungetc: ungetc_m()
if it not fail return the char of argument + carry flag ==0
if it fail return EOF carry flag set
so it seem agree with above standard
but so i can unget EOF too because i can see the carry flag that
ungetc_m return
then how it is "not standard" if i set a bit in the file->flag when i
can not unget a char (until EOF) ?
then how it is "not standard" if i set a bit in the file->flag when i
can not unget a char (until EOF) ?
The contents of the FILE struct are not defined by the C standard,
so the flag field is not certain to exist.
You may also wish to consider whether your code would continue to
be correct if you were using POSIX threads: would you need to lock
the FILE structure before changing the flag ?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.