If myscript
-like many scripts or programs- write(2)-s something (e.g. some numbers or strings) on its stdout, you might use (on POSIX) popen(3) in your program.
When successful popen
would give a FILE*
stream for <stdio.h>
routines (that you could read with fscanf
etc).
(I am guessing that you are using some Linux or POSIX system)
But system(3) catches the exit(3) status (actually also other terminations, e.g. signal(7)-s, see waitpid(2)) of myscript
. It don't care about the output of myscript
which gets inserted in the standard output of your program.
Don't forget to use pclose(3) (not fclose
) to close that popen
-ed stream. Read also pipe(7).
For example, this code reads the number from the output of the wc -l /etc/fstab
command
FILE*p = popen("wc -l /etc/fstab", "r");
if (!p) { perror("popen"); exit(EXIT_FAILURE); };
int n= 0;
if (fscanf(p,"%d", &n)<=0) { perror("fscanf"); exit(EXIT_FAILURE; };
if (pclose(p)>0) /*wc failed*/ exit(EXIT_FAILURE);
printf("your fstab has %d lines\n", n);
You should read Advanced Linux Programming (freely downloadable). You may want to use other things (e.g. some system calls like fork(2), execve(2), waitpid(2), pipe(2), dup2(2); see syscalls(2)).
There are many other ways of doing some Inter-Process Communication, including fifo(7)-s and socket(7)-s.
Beware of code injection when running, with popen
or system
, some computed string containing a command.
Some libraries (notably Qt or POCO for C++) offer other ways of running programs and communicating with them.