Jump to content
  • Advertisement
Sign in to follow this  
zedzeek

C++ outputing to two streams together

This topic is 4946 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i wanna send data to both A+B in the one call ostream A,B; A << "g"; B << "g"; how can i do it, ta zed

Share this post


Link to post
Share on other sites
Advertisement

void wrapper(ostream &a, ostream &b, const string &data)
{
a << data;
b << data;
}

Share this post


Link to post
Share on other sites

struct DoubleStream {
DoubleStream(ostream& a, ostream& b) : a_(a), b_(b) {
}
template<typename T>
DoubleStream& output(const T& object) {
a_ << object;
b_ << object;
return *this;
}
private:
ostream& a_;
ostream& b_;
};

template<typename T>
DoubleStream& operator<<(DoubleStream& ds, const T& object) {
return ds.output(object);
}


Usage

#include "DoubleStream.h"
#include <iostream>
#include <complex>

int main() {
DoubleStream ds = DoubleStream(std::cout, std::err);
ds << "Hello, World!" << ds << std::endl;
ds << 5.5 << std::endl;
ds << std::complex<double>(1.412, 16.5) << std::endl;
return 0;
}

Share this post


Link to post
Share on other sites
or even


class funcStreamer
{
funcStreamer(ostream &a, ostream &b)
: m_a(a), m_b(b) {}

template <typename DATA_TYPE>
void operator() (const DATA_TYPE& data)
{
m_a << data;
m_b << data;
}

private:
ostream &m_a;
ostream &m_b
}

// in use
ostream a, b;
funcStreamer outFunc(a, b);

outFunc("string value");


longer to write but shorter to use

Share this post


Link to post
Share on other sites
I thought I would be a little different and do something most likely not useful:


#include <utility> // std::pair
#include <iterator> // std::ostream_iterator, std::output_iterator_tag
#include <iosfwd> // std::char_traits

template < typename Tp, typename CharT = char, typename Traits = std::char_traits<CharT> >
struct pair_ostream_iterator
: std::pair< std::ostream_iterator<Tp, CharT, Traits>,
std::ostream_iterator<Tp, CharT, Traits> > {

typedef CharT char_type;
typedef Traits traits_type;
typedef std::basic_ostream<CharT, Traits> ostream_type;
typedef std::pair< first_type, second_type > pair_type;

typedef std::output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;

typedef pair_ostream_iterator<Tp, CharT, Traits> Self;

pair_ostream_iterator(ostream_type& out1, ostream_type& out2, const CharT* Delim = 0)
: pair_type(first_type(out1, Delim), second_type(out2, Delim)) {}

Self& operator=(const Tp& val) {
first = val, second = val;
return *this;
}

Self& operator*() { // pretend to return designated value
return *this;
}

Self& operator++() { // pretend to preincrement
return *this;
}

Self operator++(int) { // pretend to postincrement
return *this;
}
};


example usage:


#include <cstddef> // std::size_t
#include <cmath> // std::rand
#include <algorithm> // std::generate_n, std::copy
#include <iterator> // std::back_inserter
#include <vector> // std::vector
#include <iostream> // std::cout, std::cerr

int main() {

const std::size_t N = 10;

std::vector<int> v;

v.reserve(N);
std::generate_n(std::back_inserter(v), N, std::rand);

std::copy(v.begin(), v.end(), pair_ostream_iterator<int>(std::cout, std::cerr, "\n"));
}


[Edited by - snk_kid on May 25, 2005 6:46:10 PM]

Share this post


Link to post
Share on other sites
thanks for that but
sorry i should of been more specific i want to be able to (*)chain them together
thus only petewood's suggestion is applicable
but i get the following error
ds << std::flush;
error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'struct DoubleStream'

(*)able to input multiple data in one line eg string << float << string << flush;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!