Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: questions about using fget as waiting loop

Author: Dieter Buerssner

Date: 16:05:14 08/30/04

Go up one level in this thread


On August 29, 2004 at 15:32:20, Reinhard Scharnagl wrote:

>On August 29, 2004 at 12:04:47, Uri Blass wrote:
>
>>I have the following code similiar to tscp (tscp is using the variable line
>>instead of buffer.
>>
>>if (!fgets(buffer, 256, stdin))
>>			return;
>>
>>I have the following questions:
>>1)How is it possible to have
>>!fgets(buffer, 256, stdin)
>>
>>I understand that it happens only in case of an error but
>>I do not see how there can be an error.

One example would be, when the GUI crashed.

>May be that the sitiation is not caused by an error, but simply by an
>EOF or Ctrl^Z input when been called via a command promt.
>
>>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()
>
>In contrast to that idea fgets() is save, but please make the buffer 1 char
>longer than the parameter amount.

That is not needed.

  char buffer[SOME_SIZE]:

...

  fgets(buffer, sizeof buffer, stdin);

is often a good way to write it.


From the C Standard (sorry for the bad line breaks):

7.19.7.2 The fgets function
Synopsis
1 #include <stdio.h>
char *fgets(char * restrict s, int n,
FILE * restrict stream);
Description
2 The fgets function reads at most one less than the number of characters
                                   ^^^^^^^^
specified by n
from the stream pointed to by stream into the array pointed to by s. No
additional
characters are read after a new-line character (which is retained) or after
end-of-file. A
null character is written immediately after the last character read into the
array.
Returns
3 The fgets function returns s if successful. If end-of-file is encountered and
no
characters have been read into the array, the contents of the array remain
unchanged and a
null pointer is returned. If a read error occurs during the operation, the array
contents are
indeterminate and a null pointer is returned.

>>3)I plan to replace this code by a waiting function.
>>The point is that I need to wait passively to commands in different places
>>one case is when I do not ponder and it is the opponent move but even when I
>>ponder during the search I may want to wait(for example if I finished the
>>maximal depth).

Using fgets() seems perfectly adequate for this task.

Regards,
Dieter



This page took 0 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.