MaTaKazer MaTaKazer - 29 days ago 12
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");
}


foo-exe

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

Answer

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.