getc可以返回EOF,但ungetc无法将其发回...为什么? [英] getc can return EOF, but ungetc can't sent it back... why?

查看:62
本文介绍了getc可以返回EOF,但ungetc无法将其发回...为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello C程序员,


有人可以告诉我为什么ungetc不能发回EOF,但它是姐姐

函数getc有没问题发送给我们?对于一个文件,这可能不会产生任何影响,但是对于一个交互式终端来说,推回EOF很可能很好(因为用户不想要)两次提供EOF




getc如何将EOF发送到管道上,但是我们不能发送EOF

ungetc的管道(特别是当这个管道相同时)? ungetc接受一个

int作为它的参数,所以EOF

和任何有效字符之间不应该有任何冲突,那么原因是什么?


我意识到这可能是一个基本问题,但不幸的是我很困惑,无法在任何地方找到答案。

Hello C programmers,

Can someone tell me why ungetc can''t sent back EOF, but it''s sister
function getc has no trouble sending it to us? For a file, this might
not make a difference, but for an interactive terminal, it is probably
nice to push EOF back (because to user doesn''t want to provide an EOF
twice).

How is it getc can send EOF down it''s pipe, but we can''t send EOF down
ungetc''s pipe (especially when this pipe is the same)? ungetc takes an
int as it''s argument, so there shouldn''t be any conflict between EOF
and any valid character, so what is the reason?

I realize this might be a basic question, but unfortunately I''m
confused and can''t find the answer anywhere.

推荐答案



" TTroy" < TI ***** @ gmail.com>在消息中写道

news:11 ********************** @ l41g2000cwc.googlegr oups.com ...

"TTroy" <ti*****@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
你好C程序员,

有人能告诉我为什么ungetc不能送回EOF,但它的姐姐
函数getc没有问题发送给我们?对于一个文件,这可能没有什么区别,但对于一个交互式终端,推回EOF可能很好(因为用户不想提供EOF
两次)。

如何将EOF发送到它的管道上,但我们不能将EOF发送到ungetc的管道(特别是当这个管道是相同)? ungetc接受了一个
int作为它的论点,所以EOF
和任何有效的角色之间不应该有任何冲突,那么原因是什么呢?

我意识到这可能是一个基本问题,但不幸的是我很困惑,无法在任何地方找到答案。
Hello C programmers,

Can someone tell me why ungetc can''t sent back EOF, but it''s sister
function getc has no trouble sending it to us? For a file, this might
not make a difference, but for an interactive terminal, it is probably
nice to push EOF back (because to user doesn''t want to provide an EOF
twice).

How is it getc can send EOF down it''s pipe, but we can''t send EOF down
ungetc''s pipe (especially when this pipe is the same)? ungetc takes an
int as it''s argument, so there shouldn''t be any conflict between EOF
and any valid character, so what is the reason?

I realize this might be a basic question, but unfortunately I''m
confused and can''t find the answer anywhere.




EOF基于以下方式返回一个条件而不是基于某个值

从流中读入。 (除非存在差异时

sizeof(unsigned char)== sizeof(int))

因此,ungetc将EOF推回到<是无意义的br />
流。


-

j



EOF is returned based on a condition and not based on some value
read in from the stream. (except there is a discrepancy when
sizeof(unsigned char) == sizeof(int))
As such, it would be nonsensical for ungetc to push EOF back into
the stream.

--
j


TTroy写道:

有人能告诉我为什么ungetc不能发回EOF,但它的姐姐
函数getc没有问题发送给我们?对于一个文件,这可能没有什么区别,但对于一个交互式终端,推回EOF可能很好(因为用户不想提供EOF
两次)。

如何将EOF发送到它的管道上,但我们不能将EOF发送到ungetc的管道(特别是当这个管道是相同)? ungetc接受了一个
int作为它的论点,所以EOF
和任何有效的角色之间不应该有任何冲突,那么原因是什么呢?

我意识到这可能是一个基本问题,但不幸的是我很困惑,无法在任何地方找到答案。

Can someone tell me why ungetc can''t sent back EOF, but it''s sister
function getc has no trouble sending it to us? For a file, this might
not make a difference, but for an interactive terminal, it is probably
nice to push EOF back (because to user doesn''t want to provide an EOF
twice).

How is it getc can send EOF down it''s pipe, but we can''t send EOF down
ungetc''s pipe (especially when this pipe is the same)? ungetc takes an
int as it''s argument, so there shouldn''t be any conflict between EOF
and any valid character, so what is the reason?

I realize this might be a basic question, but unfortunately I''m
confused and can''t find the answer anywhere.




[1] c: \ c\ junk> cat junk.c

#include< stdio.h>


int main(无效)

$

int ch;


while(EOF!=(ch = getc(stdin)))继续;

if(EOF == getc(stdin))put(Still at EOF);

else puts(EOF is transient);

ungetc(ch ,stdin);

put(尝试放回EOF);

if(EOF == getc(stdin))put(Still at EOF) );

else put(EOF是暂时的);

返回0;

}


对于它的价值:


[1] c: \\ c \\\ junk> cat junk.c

#include< stdio.h>


int main()

{

int ch;


while(EOF!=(ch = getc(stdin)))继续;

if(b) EOF == getc(stdin))put(Still at EOF);

else puts(EOF is transient);

ungetc(ch,stdin) );

put(试图放回EOF);

if(EOF == getc(stdin))put(Still at EOF);

else put(EOF是短暂的);

ungetc('''',stdin);

puts(")试图放回''a''');

if(EOF == getc(stdin))put(Still at EOF);

else puts(EOF是短暂的);

返回0;

}


[1] c:\ c \ junk> gcc junk.c -o junk

[1] c:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ >
^ Z

仍然在EOF

试图放回EOF

仍在EOF

试图放回''a''

EOF是暂时的


[1] c:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ >
试图收回EOF

仍然在EOF

试图收回''a''

EOF是暂时的


-

Chuck F(cb********@yahoo.com)(cb ******** @ worldnet .att.net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>使用worldnet地址!



[1] c:\c\junk>cat junk.c
#include <stdio.h>

int main(void)
{
int ch;

while (EOF != (ch = getc(stdin))) continue;
if (EOF == getc(stdin)) puts("Still at EOF");
else puts("EOF was transient");
ungetc(ch, stdin);
puts("Tried to put back EOF");
if (EOF == getc(stdin)) puts("Still at EOF");
else puts("EOF was transient");
return 0;
}

For what it''s worth:

[1] c:\c\junk>cat junk.c
#include <stdio.h>

int main()
{
int ch;

while (EOF != (ch = getc(stdin))) continue;
if (EOF == getc(stdin)) puts("Still at EOF");
else puts("EOF was transient");
ungetc(ch, stdin);
puts("Tried to put back EOF");
if (EOF == getc(stdin)) puts("Still at EOF");
else puts("EOF was transient");
ungetc(''a'', stdin);
puts("Tried to put back ''a''");
if (EOF == getc(stdin)) puts("Still at EOF");
else puts("EOF was transient");
return 0;
}

[1] c:\c\junk>gcc junk.c -o junk

[1] c:\c\junk>junk
blah blah blah
^Z
Still at EOF
Tried to put back EOF
Still at EOF
Tried to put back ''a''
EOF was transient

[1] c:\c\junk>junk <junk.c
Still at EOF
Tried to put back EOF
Still at EOF
Tried to put back ''a''
EOF was transient

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!




TTroy写道:


TTroy wrote:
Hello C程序员,

有人能告诉我为什么ungetc不能送回EOF,但它的姐姐
函数getc没有问题发送给我们?对于一个文件,这可能没有什么区别,但对于一个交互式终端,推回EOF可能很好(因为用户不想提供EOF
两次)。

如何将EOF发送到它的管道上,但我们不能将EOF发送到ungetc的管道(特别是当这个管道是相同)? ungetc接受了一个
int作为它的论点,所以EOF
和任何有效的角色之间不应该有任何冲突,那么原因是什么呢?

我意识到这可能是一个基本问题,但不幸的是我很困惑,无法在任何地方找到答案。
Hello C programmers,

Can someone tell me why ungetc can''t sent back EOF, but it''s sister
function getc has no trouble sending it to us? For a file, this might
not make a difference, but for an interactive terminal, it is probably
nice to push EOF back (because to user doesn''t want to provide an EOF
twice).

How is it getc can send EOF down it''s pipe, but we can''t send EOF down
ungetc''s pipe (especially when this pipe is the same)? ungetc takes an
int as it''s argument, so there shouldn''t be any conflict between EOF
and any valid character, so what is the reason?

I realize this might be a basic question, but unfortunately I''m
confused and can''t find the answer anywhere.




ungetc()经常用来提前预测:你

用getc()读一个字符,检查它并决定你

真的不想读它了,用

ungetc()将其推回去。但是,ungetc()还能够将

推回一个与getc()读取不同的字符;那是'b
为什么它需要两个参数而不是一个。后者

的使用可能并不常见,但可以想象使用一个序列

的getc()调用来读入& lt;",比如说,和然后使用

ungetc(''<'',fp)来推回它的翻译。


考虑到这一点,假设你的用户键入了四十二个
字符并点击RETURN,你开始阅读它们

with getc()。由于一些奇怪的原因,你在读完十个字符后调用ungetc(EOF,fp)

。现在:下一个

getc()应该返回什么?如果它返回EOF,那就说没有

更多的输入 - 但实际上有。如果它返回除了EOF之外的其他东西,那么ungetc()调用的重点是什么? (A $ / $
幻想:也许ungetc(EOF,stdin)可能是那个 -
寻求fflush(stdin)...... ;-)


值EOF不是特例,它是一个特殊条件的报告。你温度计上的温度读数不是温度,它是温度的报告。\\ b
温度。您不能通过更改温度计上的

标记来融化雪堆,并且您无法通过操纵EOF值来影响

实际I / O条件

报告。


-
Er ** *******@sun.com



ungetc() is often used to provide a look-ahead: you
read a character with getc(), inspect it and decide you
really didn''t want to read it yet, and push it back with
ungetc(). However, ungetc() is also capable of pushing
back a different character than the one getc() read; that''s
why it takes two arguments instead of just one. The latter
use is perhaps unusual, but one can imagine using a sequence
of getc() calls to read in "&lt;", say, and then using
ungetc(''<'', fp) to "push back" its translation.

With this in mind, suppose your user types forty-two
characters and hits RETURN, and you start reading them in
with getc(). For some odd reason, you call ungetc(EOF, fp)
after reading ten characters. Now: what should the next
getc() return? If it returns EOF, it''s saying there''s no
more input -- but in fact, there is. If it returns something
other than EOF, what was the point of the ungetc() call? (A
fanciful thought: maybe ungetc(EOF, stdin) could be the oft-
sought fflush(stdin) ... ;-)

The value EOF is not an exceptional condition, it is a
report of an exceptional condition. The reading on your
thermometer is not the temperature, it is a report of the
temperature. You cannot melt the snowbanks by changing the
markings on your thermometer, and you cannot influence the
actual I/O condition by manipulating the EOF value that
reports it.

--
Er*********@sun.com


这篇关于getc可以返回EOF,但ungetc无法将其发回...为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆