[英]'Continuous' C++ output of one executable as Input to another program
I am trying to pass the output generated by one executable as input into another. 我试图将一个可执行文件生成的输出作为输入传递给另一个可执行文件。 I have been able to send in one line at a time.
我已经能够一次发送一行。
The problem is when I try to send in a 'sequence of lines generated in a while loop' from Program1 to be read as input by Program2. 问题是当我尝试从Program1发送“ while循环中生成的行序列”以由Program2读取为输入时。 I tried piping the executables in terminal (as given below) , but it fails to work.
我尝试在终端中管道可执行文件(如下所示) ,但无法正常工作。
./Program1 | ./Program2
./Program1 |xargs ./Program2
./Program1 > ./Program2
I want to avoid File I/O. 我想避免文件I / O。
Note: Platform : Linux 注意:平台: Linux
================== ==================
Something along the lines of the following example 遵循以下示例的内容
Program1 (Writing to Terminal) 程序1(写到终端)
int main(int argc, char *argv[])
{
int i = 2200;
while(1){
printf("%d \n", i);
i++;
}
}
Program2 (Reading from Terminal, the output of Program1) 程序2(从终端读取,程序1的输出)
int main(int argc, char *argv[])
{
while(1){
// Read 'i' values
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;
}
The problem is that you are trying to read the program arguments . 问题是您正在尝试读取程序参数 。 But when you pipe from one program to the next the output from the first program becomes the standard input (
std::cin
) of the second program. 但是,当您从一个程序到下一个程序进行管道传输时,第一个程序的输出将成为第二个程序的标准输入 (
std::cin
)。
Try this for program 2: 尝试此程序2:
#include <string>
#include <iostream>
int main()
{
std::string line;
while(std::getline(std::cin, line)) // read from std::cin
{
// show that it arrived
std::cout << "Line Received: " << line << '\n';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.