有人能帮我吗?
我想尝试做以下事情:
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
#include <sstream>
#include <cassert>
namespace io = boost::iostreams;
typedef io::stream<io::tee_device<std::stringstream, std::stringstream> > Tee;
std::stringstream ss1, ss2;
Tee my_split(ss1, ss2); // redirects to both streams
my_split << "Testing";
assert(ss1.str() == "Testing" && ss1.str() == ss2.str());
Run Code Online (Sandbox Code Playgroud)
但它不会在VC9中编译:
c:\lib\boost_current_version\boost\iostreams\stream.hpp(131) : error C2665: 'boost::iostreams::tee_device<Sink1,Sink2>::tee_device' : none of the 2 overloads could convert all the argument types
Run Code Online (Sandbox Code Playgroud)
有没有人得到这个工作?我知道我可以自己上课去做,但我想知道我做错了什么.
谢谢
我需要将std :: cout 的副本重定向到该文件.即我需要在控制台和文件中看到输出.如果我用这个:
// redirecting cout's output
#include <iostream>
#include <fstream>
using namespace std;
int main () {
streambuf *psbuf, *backup;
ofstream filestr;
filestr.open ("c:\\temp\\test.txt");
backup = cout.rdbuf(); // back up cout's streambuf
psbuf = filestr.rdbuf(); // get file's streambuf
cout.rdbuf(psbuf); // assign streambuf to cout
cout << "This is written to the file";
cout.rdbuf(backup); // restore cout's original streambuf
filestr.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后我将字符串写入文件,但我在控制台中看不到任何内容.我该怎么做?
在C++中,是否有一种智能方法可以将stdout的输出镜像到控制台和文件?我希望有一种方法可以像这个问题那样做.
编辑:能够只用标准库(即:没有提升)来做这件事会很好.
我在C++应用程序中使用printf()来从Linux命令行运行它时打印一些信息.现在,我正在使用输出重定向(./main> output.txt)将结果保存到文件中.我想知道是否可以同时使用两者:在程序运行时在命令行中查看结果,并进行输出重定向; 没有在C++中通过文件I/O明确地执行它.