Works great for me:
Note: std :: getline () reads a line (but not the "\ n" character, the line terminator is discarded after reading each line). But the loop will be entered once for each row.
#include <iostream>
#include <sstream>
int main()
{
std::stringstream redirectStream;
std::streambuf* oldbuf = std::cout.rdbuf( redirectStream.rdbuf() );
std::cout << "Hello1\n";
std::cout << "Hello2\n";
std::string str;
while(std::getline(redirectStream, str))
{
fprintf(stdout,"Line: %s\n",str.c_str());
}
std::cout.rdbuf(oldbuf);
}
. , - std:: cout. , streamirectstreamstream , , std:: cout, -. std:: cout , "redirectStream", , std:: cout . , .