在OSX中从C程序运行终端命令

在OSX中从C程序运行终端命令,c,macos,pipe,system,popen,C,Macos,Pipe,System,Popen,我正在尝试使用plotutils从二进制数据生成绘图。首先,我编写了一个C程序来导出一些示例二进制数据。然后,当我在终端中执行以下命令时,将按预期生成绘图 graph -T png -I d <'/Users/username/Documents/Restofpath/PlotutilsDataGen/testData'> '/Users/username/Documents/Restofpath/PlotutilsDataGen/testPlot.png' 关于我做错了什么,或者

我正在尝试使用plotutils从二进制数据生成绘图。首先,我编写了一个C程序来导出一些示例二进制数据。然后,当我在终端中执行以下命令时,将按预期生成绘图

graph -T png -I d <'/Users/username/Documents/Restofpath/PlotutilsDataGen/testData'> '/Users/username/Documents/Restofpath/PlotutilsDataGen/testPlot.png'

关于我做错了什么,或者我如何才能让它发挥作用,有什么想法吗?谢谢。

我想这可能与重定向有关。

虽然我认为系统调用是/bin/sh,但这是一个shell问题。我应该提到的是,在这两种情况下,我得到的错误都是
sh:graph:command not found
。这可能与运行
system(plotcommand)
时的
路径设置不正确有关<代码>系统();以交互方式在终端上,您可能正在使用bash,并且在.bashrc文件中,可能存在对
路径的更改,这些更改在通过
system()运行
sh
时不使用。最简单的解决方案是在
plotcommand
分配中包含
graph
的完整路径。(但接下来可能找不到库。)为什么不使用GNU
libplot
直接从代码生成绘图,而不是依赖外部命令?这是一个很大的遗漏。那可能是一个路径问题
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int numSamples = 1024;
    double outputVec[2*numSamples];
    char outputPath[200] = "/Users/username/Documents/Restofpath/PlotutilsDataGen/testData";
    char plotcommand[400] = "graph -T png -I d <'/Users/username/Documents/Restofpath/PlotutilsDataGen/testData'> '/Users/username/Documents/Restofpath/PlotutilsDataGen/testPlot.png'";
    FILE *fp, *p;

    // Compute sample functions to export
    for(int ii = 0; ii < (2*numSamples); ii = ii + 2)
    {
        outputVec[ii] = (double)ii/2;
        outputVec[ii+1] = (double)(ii*2);
    }

    // Export as binary data file for plotutils to use for plotting
    fp = fopen(outputPath, "wb");
    fwrite(outputVec, sizeof(double), 2*numSamples, fp);
    fclose(fp);

    // Option 1
    p = popen(plotcommand, "w");
    pclose(p);

    // Option 2
    system(plotcommand);

    return 0;
}