Friday, 15 August 2014

linux - How to make a bash program accept commands from another C program -



linux - How to make a bash program accept commands from another C program -

i'm new linux , programming. opened application programme c programme using system("prog");

prog-> #after opening programme

this "prog" accepts commands user , displays output.

prog-> write # accepts command user 1 2 3 4 5 # displays output prog-> # waiting next command

i want create command sent c programme instead of getting user. can't utilize system(""); send commands opened programme "prog" (as in passing commands cli c program). system(); works cli , not opened program.

is there way can send commands opened application c program?

i should store displayed output "1 2 3 4 5" in file. pls help.

superficially, might find popen() function appropriate, along pclose() function.

file *fp = popen("prog", "w");

this indicates programme write fp send info standard input of prog. standard output of prog go same place standard output of program. when you're finished, close stream pclose():

pclose(fp);

if need more command on output goes, need use:

pipe() fork() dup2() execv()

if insufficient — if prog not react not having terminal input and/or output — need investigate pseudo-ttys or pty devices. these little tricky use; they're subject separate question (and there's several relevant questions reply available on cover use).

linux bash command-line-interface

No comments:

Post a Comment