views:

47

answers:

1

I'm trying to write a C++ program in Linux that communicates with a chess engine via its command line interface. Chess engines have standard protocols like UCI so, if I could write this, I could use different chess engines interchangeably.

My C++ program should start the chess engine, send it a command, get the output, send it a command, get the output, etc... How is this done?

+1  A: 

You'll need to set up some pipes from standard in and standard out. By default, standard out from a program is written to the terminal and standard in is read from the terminal. Essentially what you'll be doing is re-routing these from the terminal to your program.

You can fork, set up the pipes, and then launch chess with execve() from your child process. This site has a simple example of how to pipe standard out from your main program to standard in of a child process:

http://www.cim.mcgill.ca/~franco/OpSys-304-427/messages/node92.html

Chris
Here are some sample function calls I ended up using: pipe(pipein), dup2(pipein[1], fileno(stdout)), close(pipein[0]), execl("/usr/games/fruit", "/usr/games/fruit", (char*) 0), read(pipein[0], buf, BSIZE), write(pipeout[1], "ucinewgame\n", 11).
jedavis