linux - How to run a C++ program in another C++ program? -
i have simple c++ program takes in inputs , outputs string. this:
$ ./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! now want write c++ program can runs c++ program , plays game automatically without user input , outputs file or standard output.
running this:
./program game r > outputfile game game program, r playing regular style.
how should this? main reason need program want automatic testing bigger program.
you use std::system <cstdlib>:
std::system("game r > outputfile"); the return value ./program's, sole argument must of type char const *.
there no standard way run program , feed standard input, though. judging command line, you're on unix variant popen <stdio.h> should work:
file *sub = popen("game r > outputfile", "w"); then write sub stdio functions , read outputfile afterwards.
(but simple testing, i'd recommend implementing core logic of program set of functions/classes can run custom main function in loop; or pick favorite scripting language handle kind of thing.)
Comments
Post a Comment