Function returning "stream" ?

Discussion in 'C++' started by StephQ, Mar 6, 2007.

  1. StephQ

    StephQ Guest

    At the moment I have a void member function (of a given class) that
    takes as input an obj of class type ofstream and write some debug
    information to it using the << operator.
    I would like to modify the code so that the function returns a
    "stream" that can be "sent" to cout or to a file using <<, as this
    would give me more flexibility.
    I used " " because I'm not sure I'm not using the correct terms
    here. :p
    Is there an easy way to accomplish this task?
    Thank you in advance!

    Cheers
    Stefano
     
    StephQ, Mar 6, 2007
    #1
    1. Advertising

  2. StephQ

    Kai-Uwe Bux Guest

    StephQ wrote:

    > At the moment I have a void member function (of a given class) that
    > takes as input an obj of class type ofstream and write some debug
    > information to it using the << operator.
    > I would like to modify the code so that the function returns a
    > "stream" that can be "sent" to cout or to a file using <<, as this
    > would give me more flexibility.
    > I used " " because I'm not sure I'm not using the correct terms
    > here. :p


    What about returning a std::string? You could send that to cout or a file
    using <<. So it seems to fit the description.

    Alternatively, you could also make your function use an ostream & instead of
    a ofstream &.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 6, 2007
    #2
    1. Advertising

  3. StephQ

    StephQ Guest

    On Mar 6, 1:39 pm, Kai-Uwe Bux <> wrote:
    > StephQ wrote:
    > > At the moment I have a void member function (of a given class) that
    > > takes as input an obj of class type ofstream and write some debug
    > > information to it using the << operator.
    > > I would like to modify the code so that the function returns a
    > > "stream" that can be "sent" to cout or to a file using <<, as this
    > > would give me more flexibility.
    > > I used " " because I'm not sure I'm not using the correct terms
    > > here. :p

    >
    > What about returning a std::string? You could send that to cout or a file
    > using <<. So it seems to fit the description.
    >
    > Alternatively, you could also make your function use an ostream & instead of
    > a ofstream &.
    >
    > Best
    >
    > Kai-Uwe Bux


    See my reply above.
    I have never used ostream before. Do you have a link to a reference /
    tutorial about it and streams in general?

    Best
    StephQ
     
    StephQ, Mar 6, 2007
    #3
  4. StephQ

    Kai-Uwe Bux Guest

    StephQ wrote:

    > On Mar 6, 1:39 pm, Kai-Uwe Bux <> wrote:
    >> StephQ wrote:
    >> > At the moment I have a void member function (of a given class) that
    >> > takes as input an obj of class type ofstream and write some debug
    >> > information to it using the << operator.
    >> > I would like to modify the code so that the function returns a
    >> > "stream" that can be "sent" to cout or to a file using <<, as this
    >> > would give me more flexibility.
    >> > I used " " because I'm not sure I'm not using the correct terms
    >> > here. :p

    >>
    >> What about returning a std::string? You could send that to cout or a file
    >> using <<. So it seems to fit the description.
    >>
    >> Alternatively, you could also make your function use an ostream & instead
    >> of a ofstream &.
    >>
    >> Best
    >>
    >> Kai-Uwe Bux

    >
    > See my reply above.


    See my reply to that reply.


    > I have never used ostream before. Do you have a link to a reference /
    > tutorial about it and streams in general?


    Unfortunately, computer books are incredibly booring to me (with very,
    _very_ few exceptions). I don't read them unless I have to look up
    something. For that, I usually refer to the C++ standard. However, it
    appears to be general consensus in this forum that this is not a good
    recommendation.

    However, for the case you mentioned, I take it that you have some function

    void log ( std::eek:fstream & the_stream ) {
    ...
    }

    I would try to just changing it to

    void log ( std::eek:stream & the_stream ) {
    // same body
    }

    Unless you use file-specific methods (for which I don't see a need), you
    should get away with that small change.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 6, 2007
    #4
  5. StephQ

    Jim Langston Guest

    "Kai-Uwe Bux" <> wrote in message
    news:esk3r2$9$...
    > StephQ wrote:
    >
    >> On Mar 6, 1:39 pm, Kai-Uwe Bux <> wrote:
    >>> StephQ wrote:
    >>> > At the moment I have a void member function (of a given class) that
    >>> > takes as input an obj of class type ofstream and write some debug
    >>> > information to it using the << operator.
    >>> > I would like to modify the code so that the function returns a
    >>> > "stream" that can be "sent" to cout or to a file using <<, as this
    >>> > would give me more flexibility.
    >>> > I used " " because I'm not sure I'm not using the correct terms
    >>> > here. :p
    >>>
    >>> What about returning a std::string? You could send that to cout or a
    >>> file
    >>> using <<. So it seems to fit the description.
    >>>
    >>> Alternatively, you could also make your function use an ostream &
    >>> instead
    >>> of a ofstream &.
    >>>
    >>> Best
    >>>
    >>> Kai-Uwe Bux

    >>
    >> See my reply above.

    >
    > See my reply to that reply.
    >
    >
    >> I have never used ostream before. Do you have a link to a reference /
    >> tutorial about it and streams in general?

    >
    > Unfortunately, computer books are incredibly booring to me (with very,
    > _very_ few exceptions). I don't read them unless I have to look up
    > something. For that, I usually refer to the C++ standard. However, it
    > appears to be general consensus in this forum that this is not a good
    > recommendation.
    >
    > However, for the case you mentioned, I take it that you have some function
    >
    > void log ( std::eek:fstream & the_stream ) {
    > ...
    > }
    >
    > I would try to just changing it to
    >
    > void log ( std::eek:stream & the_stream ) {
    > // same body
    > }
    >
    > Unless you use file-specific methods (for which I don't see a need), you
    > should get away with that small change.


    Umm.. I don't see a change. I think you meant to change it to:

    std::eek:stream& log ( std::eek:stream& the_stream ) {
    // same body
    return the_stream;
    }

    didnt' you?


    >
    >
    > Best
    >
    > Kai-Uwe Bux
     
    Jim Langston, Mar 6, 2007
    #5
  6. StephQ

    Kai-Uwe Bux Guest

    Jim Langston wrote:

    >
    > "Kai-Uwe Bux" <> wrote in message
    > news:esk3r2$9$...
    >> StephQ wrote:
    >>
    >>> On Mar 6, 1:39 pm, Kai-Uwe Bux <> wrote:
    >>>> StephQ wrote:
    >>>> > At the moment I have a void member function (of a given class) that
    >>>> > takes as input an obj of class type ofstream and write some debug
    >>>> > information to it using the << operator.
    >>>> > I would like to modify the code so that the function returns a
    >>>> > "stream" that can be "sent" to cout or to a file using <<, as this
    >>>> > would give me more flexibility.
    >>>> > I used " " because I'm not sure I'm not using the correct terms
    >>>> > here. :p
    >>>>
    >>>> What about returning a std::string? You could send that to cout or a
    >>>> file
    >>>> using <<. So it seems to fit the description.
    >>>>
    >>>> Alternatively, you could also make your function use an ostream &
    >>>> instead
    >>>> of a ofstream &.
    >>>>
    >>>> Best
    >>>>
    >>>> Kai-Uwe Bux
    >>>
    >>> See my reply above.

    >>
    >> See my reply to that reply.
    >>
    >>
    >>> I have never used ostream before. Do you have a link to a reference /
    >>> tutorial about it and streams in general?

    >>
    >> Unfortunately, computer books are incredibly booring to me (with very,
    >> _very_ few exceptions). I don't read them unless I have to look up
    >> something. For that, I usually refer to the C++ standard. However, it
    >> appears to be general consensus in this forum that this is not a good
    >> recommendation.
    >>
    >> However, for the case you mentioned, I take it that you have some
    >> function
    >>
    >> void log ( std::eek:fstream & the_stream ) {
    >> ...
    >> }
    >>
    >> I would try to just changing it to
    >>
    >> void log ( std::eek:stream & the_stream ) {
    >> // same body
    >> }
    >>
    >> Unless you use file-specific methods (for which I don't see a need), you
    >> should get away with that small change.

    >
    > Umm.. I don't see a change. I think you meant to change it to:
    >
    > std::eek:stream& log ( std::eek:stream& the_stream ) {
    > // same body
    > return the_stream;
    > }
    >
    > didnt' you?


    No, I did not. Rereading what I wrote, I find that I changed exactly what I
    meant to change. You just missed it. Hint: I removed just one letter!


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 6, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasmusson, Lars
    Replies:
    1
    Views:
    776
    popov
    Apr 30, 2004
  2. Replies:
    9
    Views:
    664
    Alex Buell
    Apr 27, 2006
  3. Alexander Korsunsky

    get stream mode flags from an opened stream

    Alexander Korsunsky, Feb 17, 2007, in forum: C++
    Replies:
    1
    Views:
    468
    John Harrison
    Feb 17, 2007
  4. StephQ
    Replies:
    3
    Views:
    335
    Kai-Uwe Bux
    Mar 6, 2007
  5. dolphin
    Replies:
    6
    Views:
    581
    Thomas Fritsch
    Mar 18, 2007
Loading...

Share This Page