fgets()和额外的字符...... [英] fgets() and extra characters...

查看:77
本文介绍了fgets()和额外的字符......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




一本我正在使用fgets()函数做的笔记的书

不是

返回,直到按下Return键或遇到EOF或其他错误。


最多(在没有EOF /错误的情况下),返回n-1个字符

plus

a附加空字符。


现在,我的问题是:


如果在调用fgets(),(比如n == 64和stdin),用户

发生

输入超过64个字符,比如128,256或者其他什么比如

,只有当用户按下Enter键时,fgets()将返回前63个字符并附加一个null


字符。


其他角色会怎样?我可以假设他们会将b
保存在libc或OS级缓冲区中,如果是这样,可以保存到多大程度?


如果是fgets(阻止按Enter键,我在

假设中是否正确

连续调用fgets()来检索剩余的字符

失败,

因为Enter只被用户按下一次?


我对fgets()在上面的行为中表现出的行为感到困惑br />
条件。没有任何书籍或标准库文档好像提供它。


谢谢。

Hi,

A book that I''m currently using notes that the fgets() function does
not
return until Return is pressed or an EOF or other error is encountered.

It then at most (in the absence of EOF/error), returns n-1 characters
plus
a appended null character.

Now, my question is:

If during a call to fgets(), (say with n == 64 and stdin), the user
happens
to enter much more than 64 characters, say 128, 256 or something like
that, fgets() would return with first 63 characters and a appended null

character only when the user presses Enter.

What would happen to the other characters? Can I assume that they would

be saved in a libc or OS level buffer and if so, upto what extent?

If fgets() blocks until Enter is pressed, am I correct in the
assumption that
successive calls to fgets() to retrieve the remaining characters would
fail,
since Enter is pressed by the user only once?

I''m confused as to the behaviour fgets() would exhibit under the above
conditions. None of the books or Standard Library documentation seem to

mention it.

Thanks.

推荐答案




santosh在12/06/05 07:21写道:


santosh wrote On 12/06/05 07:21,:


我正在使用的一本书,说明fgets()函数确实没有
返回,直到按下Return键或遇到EOF或其他错误。

最多(在没有EOF /错误的情况下),返回n-1个字符
加上
附加的空字符。

现在,我的问题是:

如果在调用fgets()期间(比如n == 64和stdin),用户
会输入超过64个字符,比如说128,256或类似的东西,只有当用户按下Enter键时,fgets()将返回前63个字符和附加的null

字符。

什么会发生在其他角色?我可以假设它们会被保存在libc或OS级缓冲区中,如果是这样,可以保存到什么程度?


fgets()尚未读取的字符仍然未读,

并且仍然在流上准备好让您阅读它们

与另一个fgets()或getc()或任何你喜欢的。


达到什么程度答案取决于操作

系统,以及流获取其输入的b / b $ b输入的设备(记住,输入不一定总是来自键盘)。 br />
有关详细信息,请参阅O / S文档,例如从键盘获得的输入行的最大长度,来自套接字,

来自磁盘文件,。 ..

如果fgets()阻塞直到Enter被按下,我是否认为
连续调用fgets()来检索剩余的字符失败,
因为用户只按了一次Enter键?


fgets()在按下Enter之前不会阻止; fgets

只是继续阅读,直到它收到换行符,填充缓冲区,或遇到文件结束或错误。 fgets()

完全没有意识到输入或任何其他键 - 或者

的鼠标,光笔,声控输入设备,你

命名它。所有这些设备的管理都是操作系统的工作,它会在感觉到fgets()时输入输入字符

。再次,请参考

O / S文档。

我对fgets()在上述条件下的行为感到困惑。没有任何书籍或标准库文档似乎都提到它。
Hi,

A book that I''m currently using notes that the fgets() function does
not
return until Return is pressed or an EOF or other error is encountered.

It then at most (in the absence of EOF/error), returns n-1 characters
plus
a appended null character.

Now, my question is:

If during a call to fgets(), (say with n == 64 and stdin), the user
happens
to enter much more than 64 characters, say 128, 256 or something like
that, fgets() would return with first 63 characters and a appended null

character only when the user presses Enter.

What would happen to the other characters? Can I assume that they would

be saved in a libc or OS level buffer and if so, upto what extent?
The characters not yet read by fgets() remain unread,
and are still on the stream ready for you to read them
with another fgets() or with getc() or whatever you like.

The "up to what extent" answer depends on the operating
system, and on the device from which the stream obtains its
input (remember, input needn''t always come from a keyboard).
Consult the O/S documentation for details like the maximum
length of input lines obtained from keyboards, from sockets,
from disk files, ...
If fgets() blocks until Enter is pressed, am I correct in the
assumption that
successive calls to fgets() to retrieve the remaining characters would
fail,
since Enter is pressed by the user only once?
fgets() does not block until Enter is pressed; fgets
just keeps on reading until it receives a newline, fills
the buffer, or encounters end-of-file or error. fgets()
is completely unaware of Enter or of any other key -- or
of mice, light-pens, voice-activated input devices, you
name it. Management of all such devices is the job of
the operating system, which will deliver input characters
to fgets() when it feels like doing so. Again, consult
the O/S documentation.
I''m confused as to the behaviour fgets() would exhibit under the above
conditions. None of the books or Standard Library documentation seem to

mention it.




我希望你的困惑比现在少。


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



I hope your confusion is less than it was.

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


santosh写道:
santosh wrote:



我正在使用的一本书,其中fgets()函数确实没有
返回,直到返回按钮或遇到EOF或其他错误。

然后最多(在没有EOF /错误的情况下),返回n-1个字符
加上
附加的空字符。

现在,我的问题是:

如果在调用fgets()期间(比如n == 64和stdin),用户会发生输入超过64个字符,比如128,256或类似<只有当用户按下Enter键时,fgets()将返回前63个字符并附加一个null

字符。

应该发生在其他角色?我可以假设它们会被保存在libc或OS级缓冲区中吗?如果是这样,可以保存到什么程度?

如果fgets()阻塞,直到Enter被按下,am我正确地假设
连续调用fgets()来检索剩余的字符会失败,
因为Enter只被用户按下一次?

我对上述
条件下fgets()表现出的行为感到困惑。


中没有提到任何书籍或标准库文档它。

Hi,

A book that I''m currently using notes that the fgets() function does
not
return until Return is pressed or an EOF or other error is encountered.

It then at most (in the absence of EOF/error), returns n-1 characters
plus
a appended null character.

Now, my question is:

If during a call to fgets(), (say with n == 64 and stdin), the user
happens
to enter much more than 64 characters, say 128, 256 or something like
that, fgets() would return with first 63 characters and a appended null

character only when the user presses Enter.

What would happen to the other characters? Can I assume that they would

be saved in a libc or OS level buffer and if so, upto what extent?

If fgets() blocks until Enter is pressed, am I correct in the
assumption that
successive calls to fgets() to retrieve the remaining characters would
fail,
since Enter is pressed by the user only once?

I''m confused as to the behaviour fgets() would exhibit under the above
conditions.
None of the books or Standard Library documentation seem to

mention it.




这篇文章中的OK_input函数:
http://groups.google.com/group/comp....3d442eaf6cc380

是最后一个我用fgets写的任何东西的时间。

它的目的是给两只在键盘上战斗的小猫提供合理的回复




现在我使用这种方法:
http://groups.google.com/group/comp。 ... ... bbc52f5be7f3be

如果rc等于0,则表示输入零长度字符串

,数组将包含垃圾。


一个警告,如果字符串长度

超出LENGTH宏,

那么多余的字符就会被吃掉。


这对文本文件也有好处:
http://groups.google.com/group/comp....e8b7bb11a2c6b8


-

pete



The OK_input function in this post:
http://groups.google.com/group/comp....3d442eaf6cc380
was the last time that I wrote anything with fgets.
It was intended to give reasonable responses
to two kittens fighting on the keyboard.

Now I use this method:
http://groups.google.com/group/comp....bbc52f5be7f3be
If rc equals 0, that means that a zero length string
was entered and the array will contain garbage.

The one caveat, is that if the string length
excedes the LENGTH macro,
then the extra characters just get eaten.

It''s good for text files too:
http://groups.google.com/group/comp....e8b7bb11a2c6b8

--
pete


santoshaécrit:
santosh a écrit :
现在,我的问题是:

如果在通话期间对于fgets(),(比如n == 64和stdin),用户
要输入超过64个字符,比如说128,256或类似
,fgets( )会回复仅当用户按Enter键时,前63个字符和附加的空
字符。


正确。

其他角色会怎样?我可以假设它们会被保存在libc或OS级缓冲区中,如果是这样,可以保存到什么程度?


它们存储在某个地方,可以通过下次调用任何输入来读取

函数,如fgets()或fgetc(),无需等待(unlesse)一个''\ n''是

读,这表示整条线终于被读了。


试试吧。

如果fgets()阻塞直到Enter被按下,我是否正确
假设
连续调用fgets()来检索剩余的字符将会失败,
因为用户只按了一次Enter键?

我对上述
条件下fgets()表现出的行为感到困惑。没有书籍或标准库文档好像

提到它。
Now, my question is:

If during a call to fgets(), (say with n == 64 and stdin), the user
happens
to enter much more than 64 characters, say 128, 256 or something like
that, fgets() would return with first 63 characters and a appended null
character only when the user presses Enter.
Correct.

What would happen to the other characters? Can I assume that they would
be saved in a libc or OS level buffer and if so, upto what extent?
They are stored somewhere, and can be read by the next call of any input
function like fgets() or fgetc() without any wait (unlesse a ''\n'' is
read, that denotes that the entire line has finally been read.

Just try it.
If fgets() blocks until Enter is pressed, am I correct in the
assumption that
successive calls to fgets() to retrieve the remaining characters would
fail,
since Enter is pressed by the user only once?

I''m confused as to the behaviour fgets() would exhibit under the above
conditions. None of the books or Standard Library documentation seem to

mention it.




实际上它已经很好地解释了K& R中的fgetc()。输入

函数基于fgetc()函数。


-

A +


Emmanuel Delahaye



Actually it''s well explained about fgetc() in the K&R. The input
functions are based on the fgetc() functions.

--
A+

Emmanuel Delahaye


这篇关于fgets()和额外的字符......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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