2009-06-16 13 views
12

Boost biblioteki C++ ma Function Template teeC++ „hello world” Zwiększ tee przykładowy program

szablonów klasy tee_filter i tee_device zapewniają dwa sposoby, aby podzielić sekwencję wyjściową tak, że wszystkie dane są kierowane jednocześnie do dwóch różnych miejscach .

Poszukuję pełnego przykładu C++ przy użyciu trójnika Boost do wyprowadzenia na standardowe wyjście i do pliku takiego jak "sample.txt".

+0

Co masz na myśli przez impuls trójnik? – stefanB

+0

tee jest jedną z bibliotek w boostu –

+0

Jakieś linki? Nie mogę znaleźć niczego powiązanego ... może to tylko ja – stefanB

Odpowiedz

26

oparciu o pomoc w kwestii John powiązać

#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(); 
} 
+0

Dzięki Matthew; miły. –

+0

Dzięki, cwhii. Gotowe. –

0

Oto przykład przy użyciu tee_filter obecnie używam do tee moje wyjście 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); 
}