Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: C and Linux

Author: Daniel Clausen

Date: 15:14:22 04/08/02

Go up one level in this thread


On April 08, 2002 at 17:53:36, Robert Hyatt wrote:

>On April 08, 2002 at 16:42:45, Frank Phillips wrote:
>
>>It does not work with the cin.rdbuf() command or (of course) polling the
>>underlying file.  I need a replacement for the cin.rdbuf, which I imaged was
>>some sort of peek command - without taking characters out of the buffer
>>
>>
>>
>>fd_set rfds;
>>struct timeval tv;
>>int retval;
>>
>>if (cin.rdbuf()->in_avail() > 1)
>>   return (1);
>>
>>FD_ZERO(&rfds);
>>FD_SET(0, &rfds);
>>tv.tv_sec = 0;
>>tv.tv_usec = 0;
>>retval = select(1, &rfds, NULL, NULL, &tv);
>>if (retval)
>>   return (1);
>
>
>If you are using C, you want to use "stdin" anyway.  If you are using C++,
>then I am not sure what you have to do there to work around the buffering
>problem...

I would suggest you're using file descriptors (instead of file handles) and use
the system calls read() and write() together with select(). This way you don't
have to worry about internal buffering.

I use something like this:

// Setup fdset
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(0, &fdset);

// Wait for input
select(16, &fdset, NULL, NULL, NULL);

// Read the stuff
char buf[256];
int nread = read(0, buf, 255);

For writing I suggest using write with similar syntax to read. (man write helps)

Works w/o any problems for me and is portable. (well, you need select(), read()
and write(), but any decent OS has that ;)

Sargon



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.