简体   繁体   English

在C ++中执行和接收mml命令的输出

[英]execute and receive the output of mml command in c++

i have an interface where i use to execute the mml command in my solaris unix like below: 我有一个接口,可用于在我的solaris Unix中执行mml命令,如下所示:

> 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. 当我在命令行上看到eaw <name>时,它将启动一个界面,在该界面中我可以执行mml命令,并且可以看到执行的命令的输出。

My idea here is to parse the command output in c++. 我的想法是解析c ++中的命令输出。 I can do away with some logic for parsing.But to start with How can get the command to be executed inside c++ ? 我可以取消一些解析逻辑。但是从如何开始在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. 这应该类似于在c ++中执行sql查询,但是我们使用其他库来执行sql查询。我也不想运行shell脚本或在两者之间创建临时文件。

what i want is to execute the command inside c++ and get the output and even that in c++. 我想要的是在c ++中执行命令并获得输出,甚至在c ++中也是如此。 could anybody give me the right directions? 有人可以给我正确的方向吗?

You have several options. 您有几种选择。 From easiest and simplest to hardest and most complex to use: 从最简单,最简单到最困难,最复杂的使用:

  • Use the system() call to spawn a shell to run a command 使用system()调用生成外壳程序以运行命令
  • Use the popen() call to spawn a subprocess and either write to its standard input stream or read from its standard output stream (but not both) 使用popen()调用可生成子流程,然后写入其标准输入流或从其标准输出流中读取(但不能同时读取两者)
  • Use a combination of pipe() , fork() , dup() / dup2() , and exec*() to spawn a child process and set up pipes for a child process's standard input and output. 使用pipe()fork()dup() / dup2()exec*()来生成子进程并为子进程的标准输入和输出设置管道。

The below code is done with the sh command. 以下代码是使用sh命令完成的。 This redirects stdout to a file named "out" which can be read later to process the output. 这会将stdout重定向到名为“ out”的文件,以后可以读取该文件以处理输出。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM