Streaming directly between streams

This is a discussion on Streaming directly between streams within the C++ Programming forums, part of the General Programming Boards category; I was wondering if you can stream directly from one stream to another. e.g. Code: ifstream f("somefile", ios::in); cout << ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Streaming directly between streams

    I was wondering if you can stream directly from one stream to another. e.g.

    Code:
    ifstream f("somefile", ios::in);
    cout << f;
    which obviously didn't work on my compiler (g++).

    I know you can use rdbuf or getline or get.

    I tried something called "tie" but that didn't work either:

    Code:
    cout.tie(&ifstream);
    So how do you go about doing this?

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Maybe this is something like what you want to do?

    Code:
    # include <fstream>
    # include <iostream>
    # include <iomanip>
    # include <iterator>
    # include <algorithm>
    using namespace std;
    
    int main()
    {
        ifstream f("test.txt",ios::in);
    
        f >> noskipws;
        std::copy(istream_iterator<char>(f), istream_iterator<char>(), ostream_iterator<char>(cout, ""));
    
        return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you're going to do that you might as well use rdbuf.

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    using namespace std;

    std::copy(...);


    Heh, just leave out the using namespace std; it is a poor habit anyway.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The ifstream, the cout and the iterators need an std:: then.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    True Daved, but my point still stands, leave out using namespace std; That is only there so that old code written prestandard can still compile.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I personally always use std::, but use of the using directive here is pretty harmless and not the point of the code at all.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Nothing wrong with using rdbuf(), you know ...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    So, I take it that there is no way to stream directly from stream to stream using the << operator? Well, the copy method was interesting enough so thanks folks.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> So, I take it that there is no way to stream directly from stream to stream using the << operator?
    There is using rdbuf, but you already knew that.

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Perhaps I should look at the specifications for this, but I was always curious why you had to use function calls to do IO on streams.

    Why won't
    Code:
    //f is an fstream
    cout << f;
    implicitly understand that I just want to dump the entire thing to cout?

    Why do you need the function calls, like:
    Code:
    //assuming ss is any type of stream
    cout << ss.rdbuf();
    //somestring would be a variable of type string
    cout << somestring.c_str();
    If anybody could throw me a few links explaining why this is so, I'd appreciate it.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    it has to do with the fact << is actually an operator and the object cout has it overloaded accepting only a limited range of right-hand operands.

    http://www.cplusplus.com/ref/iostrea...ratorltlt.html

    one of them is a stream buffer
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you could always overload the << operator yourself.

    something like
    Code:
    ostream &operator<<(ostream &os, ifstream &f)
    {
        os << f.rdbuf();
        return os;
    }
    but that doesn't really gain you much and could possibly cause an overload ambiguity (or maybe not, I'm just thinking aloud)
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> cout << somestring.c_str();
    You don't need a function call there, there is an overload for the string class already:
    Code:
    cout << somestring;
    Perhaps you forgot to #include <string>. On some compilers the string class works for the most part but fails with operator<< if you don't include the proper header.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing to two streams: va_arg issues
    By dwks in forum C Programming
    Replies: 2
    Last Post: 09-26-2007, 10:14 AM
  2. streaming audio
    By neandrake in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2004, 12:11 AM
  3. Input stream directly to structure elements?
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 04-24-2003, 11:43 AM
  4. Independent streams of pseudo random number generator
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-15-2001, 04:32 AM

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