Using python to run a C++ program and test it -


suppose have simple c++ program takes in inputs , outputs string. (actual program more complicated still text based):

$ ./game $ kind of game? type r regular, s special. $ r $ choose number 1 - 10 $ 1 $ no try again $ 2 $ no try again $ 5 $ yes win! 

i haven't used python before, possible write python script run program, feeds input, , outputs results standard output? have ask question here running using c++ seems complicated. awesome direct me code examples. appreciated.

use pexpect.

normal stdin/stdout piping doesn't work, because standard library facilities in parent , child processes tend buffer i/o more aggressively when file descriptor isn't tty (via isatty call). obviously, can fix in parent, since own code; call flush @ appropriate points. child process running preexisting code don't own. pexpect module feeds child process pseudo-tty, tricks child thinking it's talking console. same trick gui terminals xterm , rxvt use.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -