C ++ "Hello World"Boost Tee 예제 프로그램
-
14-09-2019 - |
문제
부스트 C ++ 라이브러리가 있습니다 함수 템플릿 티
클래스 템플릿 TEE_FILTER 및 TEE_DEVICE는 출력 시퀀스를 분할하는 두 가지 방법을 제공하여 모든 데이터가 두 개의 다른 위치로 동시에 지시되도록합니다.
Boost Tee를 사용하여 완전한 C ++ 예제를 찾고 있으며 Standard Out 및 "Sample.txt"와 같은 파일을 출력합니다.
해결책
John이 연결된 질문의 도움을 바탕으로 :
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
#include <fstream>
#include <iostream>
using std::ostream;
using std::ofstream;
using std::cout;
namespace bio = boost::iostreams;
using bio::tee_device;
using bio::stream;
int main()
{
typedef tee_device<ostream, ofstream> TeeDevice;
typedef stream<TeeDevice> TeeStream;
ofstream ofs("sample.txt");
TeeDevice my_tee(cout, ofs);
TeeStream my_split(my_tee);
my_split << "Hello, World!\n";
my_split.flush();
my_split.close();
}
다른 팁
다음은 사용하는 예입니다 tee_filter
현재 Tee My Boost.test 출력을 사용하고 있습니다.
{ // init code, use static streams to keep them alive until test run process end
using namespace boost::iostreams;
static ofstream ofs("boost_test_output.log.xml"); // log file
static tee_filter<ostream> fileFilt(ofs); // tee all passed data to logfile
// note derives from `boost::iostreams::output_filter`
static text_xml_readability_filter xmlFilt; // filter all passed data, making the XML output readable
static filtering_ostream filter; // master filter
filter.push(fileFilt); // 1st, tee off any data to the file (raw boost XML)
filter.push(xmlFilt); // 2nd make the xml data stream readable (linebreaks, etc.)
filter.push(cout); // 3rd output the readable XML to cout
boost::unit_test::unit_test_log.set_stream( filter );
}
제휴하지 않습니다 StackOverflow