i have an interface where i use to execute the mml command in my solaris unix like below:
> eaw 0004
<RLTYP;
BSC SYSTEM TYPE DATA
GSYSTYPE
GSM1800
END
<
As soon as i do eaw <name>
on the command line.It will start an interface where in i can execute mml commands and i can see the output of those commands executed.
My idea here is to parse the command output in c++. I can do away with some logic for parsing.But to start with How can get the command to be executed inside c++ ? Is there any predefined way to do this. This should be similar to executing sql queries inside c++.But we use other libraries to execute sql queries.I also donot want to run a shell script or create temporary files in between.
what i want is to execute the command inside c++ and get the output and even that in c++. could anybody give me the right directions?
You have several options. From easiest and simplest to hardest and most complex to use:
system()
call to spawn a shell to run a command popen()
call to spawn a subprocess and either write to its standard input stream or read from its standard output stream (but not both) pipe()
, fork()
, dup()
/ dup2()
, and exec*()
to spawn a child process and set up pipes for a child process's standard input and output. The below code is done with the sh
command. This redirects stdout
to a file named "out" which can be read later to process the output. Each command to the process can be written through the pipe.
#include <stdio.h>
int main()
{
FILE *fp;
fp = popen("sh > out", "w");
if (fp) {
fprintf(fp, "date\n");
fprintf(fp, "exit\n");
fclose(fp);
}
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.