2014-11-30 35 views
5

Sto scrivendo un codice in cui devo stampare gli stessi dati sulla console e su un file. C'è un modo per popolare un oggetto di flusso di output comune e quindi visualizzarlo su console usando cout ed esportarlo in un file, usando le librerie fstream e iostream?C'è un modo per creare un oggetto flusso di output comune da stampare sulla console e su un file in C++?

+3

Utilizzare un [ 'boost :: :: iostreams tee_device'] (http://www.boost.org/doc/libs/ 1_39_0/libs/iostreams/doc/funzioni/tee.html). – 0x499602D2

+0

Si consiglia di farlo nell'ambiente di chiamata. –

risposta

6

Sicuro. Dovresti semplicemente creare un buffer di flusso adatto che probabilmente memorizza su altri buffer di flusso che scrive internamente. Utilizzando questo buffer di flusso, devi creare un std::ostream a cui stai scrivendo.

Per esempio, ecco un semplice implementazione di questo approccio:

#include <streambuf> 
#include <ostream> 

class teebuf 
    : public std::streambuf 
{ 
    std::streambuf* sb1_; 
    std::streambuf* sb2_; 

    int overflow(int c) { 
     typedef std::streambuf::traits_type traits; 
     bool rc(true); 
     if (!traits::eq_int_type(traits::eof(), c)) { 
      traits::eq_int_type(this->sb1_->sputc(c), traits::eof()) 
       && (rc = false); 
      traits::eq_int_type(this->sb2_->sputc(c), traits::eof()) 
       && (rc = false); 
     } 
     return rc? traits::not_eof(c): traits::eof(); 
    } 
    int sync() { 
     bool rc(false); 
     this->sb1_->pubsync() != -1 || (rc = false); 
     this->sb2_->pubsync() != -1 || (rc = false); 
     return rc? -1: 0; 
    } 
public: 
    teebuf(std::streambuf* sb1, std::streambuf* sb2) 
     : sb1_(sb1), sb2_(sb2) { 
    } 
}; 

class oteestream 
    : private virtual teebuf 
    , public std::ostream { 
public: 
    oteestream(std::ostream& out1, std::ostream& out2) 
     : teebuf(out1.rdbuf(), out2.rdbuf()) 
     , std::ostream(this) { 
     this->init(this); 
    } 
}; 

#include <fstream> 
#include <iostream> 

int main() 
{ 
    std::ofstream fout("tee.txt"); 
    oteestream tee(fout, std::cout); 
    tee << "hello, world!\n"; 
} 
+1

Un buffer di flusso personalizzato potrebbe essere eccessivo qui. Salvare in un 'stringstream' e emetterlo due volte potrebbe essere abbastanza buono. Comunque, buono. (A proposito: hai avuto quel codice in giro?) – Deduplicator

+1

@Deduplicator: sì, c'è una soluzione che è una PITA da usare e una soluzione adeguata che è piacevole da usare. Il wrapping del buffer del flusso in un 'std :: ostream' rende il buffer del tee stream abbastanza piacevole da usare. ... e, sì, avevo questo codice in giro: non sto digitando _that_ fast :-) –