Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: questions about using fget as waiting loop

Author: Bas Hamstra

Date: 12:39:48 08/29/04

Go up one level in this thread


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.

Here is the Borland documentation:

Syntax

#include <stdio.h>
char *fgets(char *s, int n, FILE *stream);
wchar_t *fgetws(wchar_t *s, int n, FILE *stream); // Unicode version

Description

Gets a string from a stream.

fgets reads characters from stream into the string s. The function stops reading
when it reads either n - 1 characters or a newline character whichever comes
first. fgets retains the newline character at the end of s. A null byte is
appended to s to mark the end of the string.

Return Value

On success fgets returns the string pointed to by s; it returns NULL on
end-of-file or error.

>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()

As long as you buffer is large enough it it safe. It just takes a line from
stdin and puts it in a string variable.

>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).
>
>Today movei has no waiting function and in case of finishing to search during
>pondering it simply unmake the pondered move and go to the main loop.
>
>I think that it is better not to finish the search in case of pondering and
>simply call wait during the search to wait passively to winboard command when
>wait may call a function that read wb input during searching(can be ponder mode
>play mode or analysis mode).
>
>Do you have a waiting function in your code?
>
>Uri

You can chose 2 approaches:

a) use a thread to check for i/0
b) use a search-interrupt

I think your waiting functions refers  to a)? Personally I use b) because it's
more portable. a) is easier once you have figured out threads.

Bas.







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.