问题
I want convert text output from one program to Gui output and first program produce line output every microsecond. If I send with pipe command in linux, second program how receive line by line and process that? in another word I have main function in C++ that this parameters is stream and unlimited. Thank you
回答1:
Program1:
#include <iostream>
int main()
{
std::cout << "Program1 says Hello world!" << std::endl; // output to standard out using std::cout
}
Program2:
#include <iostream>
#include <string>
int main()
{
std::string line;
while (std::getline(std::cin, line)) // Read from standard in line by line using std::cin and std::getline
{
std::cout << "Line received! Contents: " << line << std::endl;
}
}
Now if you run Program1
and pipe into Program2
, you should get:
$ ./Program1 | ./Program2
Line Recieved! Contents: Program1 says Hello world!
Note that Program2
will keep reading from standard input until EOF
is reached (or some error occurs).
For your case, Program1
is the thing that generates the output, and Program2
is the GUI program that consumes the output. Note that, in order for the program to be non-blocking, you'll probably want to do your reading from standard input on a separate thread.
As for your constraint of "receiving input every microsecond" that may not be realistic... It will process as fast as it can, but you can't rely on standard in/out to be that fast, especially if you're sending a decent amount of data.
来源:https://stackoverflow.com/questions/13779462/c-stream-input-process