c++ - 使用 `backticks` 而不是 Pipe | 读取值C++

标签 c++ linux shell

在 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 生成的输出读取到我的程序中?

最佳答案

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

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

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

关于c++ - 使用 `backticks` 而不是 Pipe | 读取值C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13510315/

相关文章:

c++ - Boost.ASIO 如何将链与 c++20 协程一起使用

c++ - 预增量运算符的放置在这里会有所不同吗?

bash - 解析顺序和扩展顺序之间的区别

shell - 在 shell 脚本中引用当前目录

macos - 从命令行打开一个 html 页面到 Mac OS 上的 anchor

c++ - 如何在模板元程序中做短路条件?

c++ - 原子类型的大小

linux - 删除所有 node_modules 子文件夹

python - 寻找有关如何为 Gnome/Ubuntu 开发小程序的建议

linux - 如何 move 和编号文件?