Array of Streams

This is a discussion on Array of Streams within the C++ Programming forums, part of the General Programming Boards category; Hello, this is my first post here. I am a novice C++ programmer, and I've come upon a problem that ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    Array of Streams

    Hello, this is my first post here.

    I am a novice C++ programmer, and I've come upon a problem that I don't know how to solve. What I'm trying to do is send some output first to cout and then to a file. The obvious way to do this would be something like:
    Code:
    #include "iostream"
    #include "fstream"
    ...
    	ofstream Log("./Log");
    	cout << "Some text." << endl;
    	Log << "Some text." << endl;
    	Log.close();
    ...
    However, if "Some text" is a whole bunch of output then this is a lot of code duplication. What I'd like to do is define an array of streams that has 2 elements. The first is cout and the second is my file stream. Then I'd have a loop over this stream array, and write the same stuff to each stream.

    But I can't quite get it to work. The best I've been able to come up with (after some scanning of the internet) is this:
    Code:
    #include "iostream"
    #include "fstream"
    ...
    	ofstream Log("./Log");
    	ostream s[2];
    	s[0].rdbuf(cout.rdbuf());
    	s[1].rdbuf(Log.rdbuf());
    
    	int i;
    	for(i=0; i<2; i++){
    		s[i] << "Some text." << endl;
    		s[i] << "Some more text." << endl;
    	}
    ...
    This almost works. It sends everything to cout just as it should, but it chokes at the first "endl" when sending to my file, so that the file simply contains "Some text.".

    What am I doing wrong, and is there a more elegant way of accomplishing this?

    Thanks very much for any and all help.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41
    This works, although it may look clumsy and undecipherable to a novice. It's how I'd do it, for better or worse.

    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    
    int main()
    {
        std::vector<std::ostream*> streams;
        std::ofstream outfile("out.txt", std::ios::out);
        streams.push_back(&std::cout);
        streams.push_back(&outfile);
    
        for (std::vector<std::ostream*>::iterator it = streams.begin(); it != streams.end(); ++it)
        {
            **it << "Some text." << std::endl;
            **it << "Some more text." << std::endl;
        }
        return 0;
    }
    It's very easy to expand on this, if you'd want to feed the data into more ostreams - just do another streams.push_back()... no need to keep count on array sizes and boundaries.
    Last edited by Oysterman; 04-06-2007 at 02:53 PM.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    2
    Quote Originally Posted by Oysterman View Post
    This works, although it may look clumsy and undecipherable to a novice. It's how I'd do it, for better or worse.
    I understood it perfectly well, and it works fine when I adapt it into my code. Problem solved.

    Thanks very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21