MaTaKazer MaTaKazer - 1 year ago 85
C Question

How to suppress prompt and supply an input therafter

I am not sure how to phrase this. But I have a C Program that will call another C Program in unix. After the call, the program(foo-exe) will prompt for an input "Y/N". I would like to suppress(hide) this prompting message and enter 'Y' thereafter as input. How do I go about doing that?

#include <stdio.h>
int main(int argc, char* argv[])
system("/home/xyz/foo-exe bar");


// do some other stuff
char ans = 'n';
printf("prompt message. Yes or no? (Y/N) ");
if(ans == 'Y') system("foo");

Answer Source

If that's all (Y or N) the foo-exe expects then you could redirect its stdin to read from a pipe.

system("echo Y | /home/xyz/foo-exe bar");

But be aware of the pitfalls of system() and its use is generally discouraged.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download