【发布时间】:2014-09-27 23:19:06
【问题描述】:
我正在寻找一种将程序一的输出传递给程序二的输入的方法。我创建了两个非常简单的程序来尝试了解如何执行此操作,但我遇到了问题。
程序一:
#include <iostream>
int main()
{
using namespace std;
std::cout << "Hello World!";
return 0;
}
ProgramTwo(打印所有参数):
#include <iostream>
int main(int argc, char *argv[])
{
using namespace std;
cout << "There are " << argc << " arguments:" << endl;
// Loop through each argument and print its number and value
for (int nArg=0; nArg < argc; nArg++)
cout << nArg << " " << argv[nArg] << endl;
return 0;
}
我已经尝试使用管道命令来传递输出:
./one | ./two
但我的第二个程序只打印一个参数:
There are 1 arguments:
0 ./two
我也试过以下命令:
/one > ./two
但这会导致两个程序都没有输出。
我哪里错了?
【问题讨论】:
-
标准输入不通过参数传递,使用
cin(类似于cout)。顺便说一句,/one > ./two只是将“一”输出覆盖到“二”文件中(不再可执行)。
标签: c++ linux input output communication