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
    StephQ
     
    StephQ, Mar 6, 2007
    #1
    1. Advertising

  2. StephQ

    Rolf Magnus 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


    Well, cout and the file actually are the streams.

    > Is there an easy way to accomplish this task?


    What you can do is simply return a string that contains the debug
    information. Use a stringstream within your function to fill the string.
     
    Rolf Magnus, Mar 6, 2007
    #2
    1. Advertising

  3. StephQ

    StephQ Guest

    > What you can do is simply return a string that contains the debug
    > information. Use a stringstream within your function to fill the string.


    The problem (is it?) is that the debug information consists of more
    than 1 line of information.
    I know that with getline you can at most get an entire line bot not
    more.
    Does the stringstream library solves the problem?

    Thanks
    StephQ

    p.s. sorry for the initial double post!
     
    StephQ, Mar 6, 2007
    #3
  4. StephQ

    Kai-Uwe Bux Guest

    StephQ wrote:

    >> What you can do is simply return a string that contains the debug
    >> information. Use a stringstream within your function to fill the string.

    >
    > The problem (is it?) is that the debug information consists of more
    > than 1 line of information.


    And why is that a problem? A std::string may contain any number of
    end-of-line characters.

    > I know that with getline you can at most get an entire line bot not
    > more. Does the stringstream library solves the problem?


    You can write multiple lines into a stringstream. The underlying string will
    contain the appropriate end-of-line characters. Writing this string to any
    stream will write separate lines.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 6, 2007
    #4
    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:
    785
    popov
    Apr 30, 2004
  2. Replies:
    9
    Views:
    676
    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:
    479
    John Harrison
    Feb 17, 2007
  4. StephQ
    Replies:
    5
    Views:
    353
    Kai-Uwe Bux
    Mar 6, 2007
  5. dolphin
    Replies:
    6
    Views:
    596
    Thomas Fritsch
    Mar 18, 2007
Loading...

Share This Page