如何使用C中的popen传递多个命令? [英] How to pass multiple commands using popen in C?

查看:146
本文介绍了如何使用C中的popen传递多个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用绘制 GNUPLOT 这是一个命令行界面图。但我需要将它集成在C程序中,因此,如果计划得到执行,图形绘制。这可以通过使用的popen 命令来完成。我已经做了code,其中我做的的popen(gnuplot的,R)所以现在当我执行程序,gnuplot的开始。但我需要发送类似的popen多个命令(罪(X),R)的popen(gnuplot的,R )所以,当我执行code赎罪图形绘制。但我不知道如何通过多个commands.Please告诉我怎样可以通过使用多个命令的popen 。请帮助谢谢?

下面是code,我现在用发送一条命令:

 的#include<&stdio.h中GT;诠释的main()
{
    FILE * FP;
    INT状态;
    FP =的popen(gnuplot的,R);    pclose函数(FP);    返回0;
}


解决方案

一旦你叫的popen(),您的文件描述符FP是开放的,允许你写通过它的数据该gnuplot的命令,会看到作为输入。请注意,类型应该是要与管做什么,而不是该命令将执行它,所以你应该'W',因为你想写使用。而直到你完成,你可以发出顺序多个命令。

例如:

 的#include<&stdio.h中GT;诠释的main()
{
    FILE * FP;
    INT状态;
    FP =的popen(gnuplot的,宽);
    fprintf中(FP,阴谋罪(X)\\ n);
    fprintf中(FP,情节棕褐色(X)\\ n);    pclose函数(FP);    返回0;
}

将发送SIN(X)和棕褐色(X)然后通过管道换行符在那里的gnuplot可以读取它作为输入。

I am trying to plot graphs using GNUPLOT which is a command line interface. But I need to integrate it in c program, so that if program gets executed, graph is plotted. This can be done by using popen command. I have made a code where I am doing popen("gnuplot","r") so now when I execute the program, gnuplot starts. But I need to send multiple commands like popen("sin(x)","r") after popen("gnuplot","r") so that a sin graph is plotted when I execute the code. But i dont know how to pass multiple commands.Please tell me how can I pass multiple commands using popen.Please help thanks?

Here is the code which I am using to send single command:

#include <stdio.h>

int main()
{
    FILE *fp;
    int status;
    fp = popen("gnuplot","r");

    pclose(fp);

    return 0;
}

解决方案

Once you have called popen(), your file descriptor 'fp' is open and allows you to write data through it which the gnuplot command will see as input. Note that the type should be what you want to do with the pipe, not what the command will do with it, so you should use 'w' since you want to write. And you can issue multiple commands in sequence until you're done.

For example:

#include <stdio.h>

int main()
{
    FILE *fp;
    int status;
    fp = popen("gnuplot","w");
    fprintf(fp, "plot sin(x)\n");
    fprintf(fp, "plot tan(x)\n");

    pclose(fp);

    return 0;
}

Will send "sin(x)" and "tan(x)" followed by newlines through the pipe where gnuplot can read it as input.

这篇关于如何使用C中的popen传递多个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆