Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: questions about using fget as waiting loop

Author: Robert Hyatt

Date: 07:20:05 08/30/04

Go up one level in this thread


On August 30, 2004 at 05:27:46, Alessandro Scotti wrote:

>On August 29, 2004 at 19:59:40, Robert Hyatt wrote:
>
>>On August 29, 2004 at 17:44:16, Alessandro Scotti wrote:
>>
>>>On August 29, 2004 at 12:04:47, Uri Blass wrote:
>>>
>>>>2)Is it safe to use fgets()?
>>>>I remember that it is not safe to use gets() and I think that for the same
>>>>reason it is not safe to use fgets()
>>>
>>>fgets() is safe. The difference with gets() is that the latter doesn't take the
>>>buffer size as a parameter, so it can overflow your buffer. Other minor
>>>difference s are that gets() removes end of line characters from the string,
>>>while fgets() leaves them there, and also that gets() can only read from stdin.
>>
>>
>>It isn't safe from the point of view of being able to ask "has anything been
>>typed on the keyboard" because fgets() uses library buffering which hides input
>>from the operating system's "is there input" query.
>
>I assumed safe referred to security here. For the rest, it's not too bad to get
>input only after "Enter" is pressed, I've recently switched to this
>line-oriented mode and so far I'm quite happy with it as it's quite portable and
>simple to code.


What do you do about the case where you type "command"<CR>  "command"<CR> (two
commands) before your program does the "check for input"?  You do the first
fgets() and the C library sucks both commands into its internal buffer, by
reading the data from the O/S, and it gives you the first command.  Now when you
ask "is there more input" you are asking the O/S and it says "no" even though
the C library has the second command in its buffer.  This is a _common_ problem
when people first modify their engines to run with winboard/xboard.  It is such
a problem that Tim and I wrote a lot of text to explain why this is a problem
and how to get around it...



This page took 0.01 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.