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