ungetc为什么有一个参数来指定要推回的字符?

huangapple go评论70阅读模式
英文:

Why does ungetc have a parameter to specify which character to push back?

问题

In my current understanding, you're supposed to call ungetc when you want to "un-get" the most recent character that you got from the stream, sort of like reversing the effect of an equivalent call to fgetc. If that's the case, then what's the point of having a parameter to specify which character to put back? Shouldn't the stream remember at least what its most recently obtained character was?

我目前的理解是,当你想要“取消获取”从流中获取的最近字符时,应该调用 ungetc,有点像是撤销对 fgetc 的等效调用的效果。如果是这样的话,那么有必要有一个参数来指定要放回哪个字符吗?流难道不应该至少记住它最近获取的字符是什么吗?

I was thinking that it could be just ungetc(stream), which probably makes more sense. I've seen a lot of implementations that involve the use of ungetc, and so far, none of them have really shown that the function is capable of pushing back a different character and explain how this behavior can be useful.

我在想,可能只需要 ungetc(stream),这可能更有意义。我看过很多涉及使用 ungetc 的实现,到目前为止,没有一个真正展示了这个函数能够推回不同的字符并解释这种行为如何有用。

英文:

In my current understanding, you're supposed to call ungetc when you want to "un-get" the most recent character that you got from the stream, sort of like reversing the effect of an equivalent call to fgetc. If that's the case, then what's the point of having a parameter to specify which character to put back? Shouldn't the stream remember at least what its most recently obtained character was?

I was thinking that it could be just ungetc(stream), which probably makes more sense. I've seen a lot of implementations that involve the use of ungetc, and so far, none of them have really shown that the function is capable of pushing back a different character and explain how this behavior can be useful.

答案1

得分: 4

In my current understanding, you're supposed to call ungetc when you want to "un-get" the most recent character that you got from the stream,

That is the use implied by the function's name, yes. And in my personal experience, it is the most common use, though this is a rarely-used function overall.

Shouldn't the stream remember at least what its most recently obtained character was?

It could, but I don't see how you get from there to should. The possibility of an alternative design for rarely-used ungetc() is pretty much the only reason for considering that.

I was thinking that it could be just ungetc(stream), which probably makes more sense.

It would make more sense if the function were limited to pushing back only the character most recently read from the specified stream. But in fact, there is no such limitation.

I've seen a lot of implementations that involve the use of ungetc, and so far, none of them have really shown that the function is capable of pushing back a different character

I don't find it surprising that the ungetc() uses you have seen all push back the character that was just read. That is certainly the primary intended use case, as reflected in the function's name. But that you haven't seen examples of other uses and can't think of any is irrelevant. The function's documentation specifies that it is the character passed as an argument that is pushed back, regardless of what the most recently read character was, or if there even was one.

Possibly this design reflects a convergence of multiple goals of C's designers:

  • memory conservation - why make every FILE in the system take an additional byte of RAM, of which there may be only a few thousand overall, just to support a rarely-used function?

  • flexible interface - why create a system interface that can do only one specific thing when it could instead do a more general thing for little or no additional cost?

英文:

> In my current understanding, you're supposed to call ungetc when you want to "un-get" the most recent character that you got from the stream,

That is the use implied by the function's name, yes. And in my personal experience, it is the most common use, though this is a rarely-used function overall.

> Shouldn't the stream remember at least what its most recently obtained character was?

It could, but I don't see how you get from there to should. The possibility of an alternative design for rarely-used ungetc() is pretty much the only reason for considering that.

> I was thinking that it could be just ungetc(stream), which probably makes more sense.

It would make more sense if the function were limited to pushing back only the character most recently read from the specified stream. But in fact, there is no such limitation.

> I've seen a lot of implementations that involve the use of ungetc, and so far, none of them have really shown that the function is capable of pushing back a different character

I don't find it surprising that the ungetc() uses you have seen all push back the character that was just read. That is certainly the primary intended use case, as reflected in the function's name. But that you haven't seen examples of other uses and can't think of any is irrelevant. The function's documentation specifies that it is the character passed as an argument that is pushed back, regardless of what the most recently read character was, or if there even was one.

Possibly this design reflects a convergence of multiple goals of C's designers:

  • memory conservation - why make every FILE in the system take an additional byte of RAM, of which there may be only a few thousand overall,<sup>*</sup> just to support a rarely-used function?

  • flexible interface - why create a system interface that can do only one specific thing when it could instead do a more general thing for little or no additional cost?


<sup>*</sup>Early C development was largely done on the PDP-11, whose minimal configuration featured a whopping 4 KB of RAM. Though I think Bell Labs' was somewhat more generously configured.

答案2

得分: 1

你可以在调用 ungetc 时使用任何字符。并不一定要与上次读取的字符相同。

这意味着该函数具有更一般的行为。

更一般的函数具有更多的应用场景。

例如,如果上次读取的字符是制表符 '\t',你可以将其替换为空格字符 ' ' 在输入流中。

英文:

You may use any character in the call of ungetc. It is not necessary that it must be the same as the last read character.

That is the function has a more general behavior.

More general functions have more applications.

For example if the last read character is tab character &#39;\t&#39; you can substitute it for space character &#39; &#39; in the input stream.

huangapple
  • 本文由 发表于 2023年5月21日 22:50:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定