【问题标题】:Read values using `backticks` instead of Pipe | C++使用 `backticks` 而不是 Pipe | 读取值C++
【发布时间】:2012-11-22 10:11:02
【问题描述】:

在 shell 上,可以使用管道将输出提供给另一个程序。

例如 :::

ps axu | grep someprocess

现在我想编写一个也接受这些管道的 C++ 程序。

我找到了类似的解决方案。

using namespace std;

int main()
{
    string mypipe;
    if(cin);
    {
        cin >> mypipe;
        cout << mypipe << endl;
    }
    return 0;
}

现在我想要,我可以使用反引号调用我的函数。

例如,我正在使用这样的 shell 构造。

./myprog.bin `./otherprog.bin someparameter`

如何将 otherprog.bin 生成的输出读取到我的程序中,而不是使用参数?

【问题讨论】:

    标签: c++ linux shell


    【解决方案1】:

    将命令行参数添加到您的主函数中,如下所示:

    int main( int argc, const char* argv[] )
    

    argc 是参数计数,argv 是保存它们的表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2020-07-15
      • 2017-09-01
      • 2016-01-03
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      相关资源
      最近更新 更多