4

たとえば、2 つのオブジェクトで抽出演算子を使用して、構文のショートカットのために同じデータを 2 つのオブジェクトに送信する場合

(out_file,  cout) << "\n\nTotal tokens found: " << statistics[0] << "\n\nAlphaNumeric Tokens: " << statistics[1]
                << "\n\nPunctuation character found: " << statistics[2] << "\nNumber of whitespace: " << statistics[3]
                << "\nNumber of newlines: " << statistics[4] << "\n\nTokens are stored in out file\n\nPress Enter to exit....";

それでは、データは out_file と cout に同時に適用されますか? out_file は fstream..

4

3 に答える 3

1

を使用して、ストリームのペアにデータを送信できますboost::iostreams::tee_device

ティーイング.cpp

#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/tee.hpp>

#include <fstream>
#include <iostream>

int main()
{
    typedef boost::iostreams::tee_device<std::ostream, std::ostream> Tee;
    typedef boost::iostreams::stream<Tee> TeeStream;

    std::ofstream out_file("./out_file.log");
    Tee tee(std::cout, out_file);

    TeeStream both(tee);

    both << "This is a test!" << std::endl;
}

建てる:

> clang++ -I/path/to/boost/1.54.0/include teeing.cpp -o teeing

走る:

> ./teeing
This is a test!

確認:

> cat ./out_file.log 
This is a test!
于 2013-11-10T07:40:06.213 に答える