3
我如何捕獲將發送到標準輸出在c + +的數據?如何捕獲將被髮送到標準輸出的數據?
我發現這裏:
// This can be an ofstream as well or any other ostream
std::stringstream buffer;
// Save cout's buffer here
std::streambuf *sbuf = std::cout.rdbuf();
// Redirect cout to our stringstream buffer or any other ostream
std::cout.rdbuf(buffer.rdbuf());
std::cout << "Hello!";
// When done redirect cout to its old self
std::cout.rdbuf(sbuf);
std::cout << "STD data: \n";
std::cout << buffer.get();
這是行不通的。 '你好'在'STD data:'之前仍然輸出,並且buffer.get()
返回'-1'。怎麼了?
仍爲空字符串。 – Ockonal 2011-04-16 14:59:42
@Ockonal:這是行得通的。請參閱鏈接:http://ideone.com/W8mW8 – Nawaz 2011-04-16 15:02:37