gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:48:41 36 4
gpt4 key购买 nike

在 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/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com