Use cout as ofstream object

Discussion in 'C++' started by Joe Hesse, Sep 26, 2007.

  1. Joe Hesse

    Joe Hesse Guest

    Hi,

    I have a C++ function that writes to an ofstream object.
    I would like to sometimes use it to write to cout.
    I realize that cout is of type ostream which is not ofstream.
    Since cout is "kind of" an output file, there should be someway to do it.
    The following code shows what I am trying to do.
    ------------------------------------------------
    #include <iostream>
    #include <fstream>
    using namespace std;

    void foo(ofstream & out)
    {
    out << "Testing";
    }

    int main()
    {
    ofstream file("test.txt");
    foo(file); // works
    file.close();

    foo(cout); // DOESN'T WORK

    return 0;
    }
    ------------------------------------------------

    Any suggestions would be appreciated.
    Thank you,
    Joe Hesse
     
    Joe Hesse, Sep 26, 2007
    #1
    1. Advertising

  2. Joe Hesse

    Barry Guest

    Joe Hesse wrote:
    > Hi,
    >
    > I have a C++ function that writes to an ofstream object.
    > I would like to sometimes use it to write to cout.
    > I realize that cout is of type ostream which is not ofstream.


    http://www.cplusplus.com/reference/iostream/

    > Since cout is "kind of" an output file, there should be someway to do it.
    > The following code shows what I am trying to do.
    > ------------------------------------------------
    > #include <iostream>
    > #include <fstream>
    > using namespace std;
    >
    > void foo(ofstream & out)


    ostream& out

    > {
    > out << "Testing";
    > }
    >
    > int main()
    > {
    > ofstream file("test.txt");
    > foo(file); // works
    > file.close();
    >
    > foo(cout); // DOESN'T WORK
    >
    > return 0;
    > }



    --
    Thanks
    Barry
     
    Barry, Sep 26, 2007
    #2
    1. Advertising

  3. Joe Hesse wrote:
    > I have a C++ function that writes to an ofstream object.
    > I would like to sometimes use it to write to cout.
    > I realize that cout is of type ostream which is not ofstream.
    > Since cout is "kind of" an output file,


    It isn't. It's an output _stream_, not file.

    > there should be someway to do
    > it. The following code shows what I am trying to do.
    > ------------------------------------------------
    > [..]


    Rewrite your "C++ function" to take an 'ostream' instead.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 26, 2007
    #3
  4. Joe Hesse

    Baby Lion Guest

    On Sep 26, 10:13 pm, "Victor Bazarov" <> wrote:
    > Joe Hesse wrote:
    > > I have a C++ function that writes to an ofstream object.
    > > I would like to sometimes use it to write to cout.
    > > I realize that cout is of type ostream which is not ofstream.
    > > Since cout is "kind of" an output file,

    >
    > It isn't. It's an output _stream_, not file.
    >
    > > there should be someway to do
    > > it. The following code shows what I am trying to do.
    > > ------------------------------------------------
    > > [..]

    >
    > Rewrite your "C++ function" to take an 'ostream' instead.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
     
    Baby Lion, Sep 26, 2007
    #4
  5. Joe Hesse

    Baby Lion Guest

    hi ,then why can it pass the compiler?
    thanks

    On Sep 26, 10:12 pm, Barry <> wrote:
    > Joe Hesse wrote:
    > > Hi,

    >
    > > I have a C++ function that writes to an ofstream object.
    > > I would like to sometimes use it to write to cout.
    > > I realize that cout is of type ostream which is not ofstream.

    >
    > http://www.cplusplus.com/reference/iostream/
    >
    > > Since cout is "kind of" an output file, there should be someway to do it.
    > > The following code shows what I am trying to do.
    > > ------------------------------------------------
    > > #include <iostream>
    > > #include <fstream>
    > > using namespace std;

    >
    > > void foo(ofstream & out)

    >
    > ostream& out
    >
    > > {
    > > out << "Testing";
    > > }

    >
    > > int main()
    > > {
    > > ofstream file("test.txt");
    > > foo(file); // works
    > > file.close();

    >
    > > foo(cout); // DOESN'T WORK

    >
    > > return 0;
    > > }

    >
    > --
    > Thanks
    > Barry
     
    Baby Lion, Sep 26, 2007
    #5
  6. Joe Hesse

    Salt_Peter Guest

    On Sep 26, 10:02 am, "Joe Hesse" <> wrote:
    > Hi,
    >
    > I have a C++ function that writes to an ofstream object.
    > I would like to sometimes use it to write to cout.
    > I realize that cout is of type ostream which is not ofstream.
    > Since cout is "kind of" an output file, there should be someway to do it.


    There is, the base class involved is std::eek:stream, isn't it? Why write
    an interface that is locked to std::eek:fstream when the same code can be
    reused to accept any derivative of std::eek:stream, including
    std::eek:stringstream, std::eek:fstream and std::cout ?

    > The following code shows what I am trying to do.
    > ------------------------------------------------
    > #include <iostream>
    > #include <fstream>
    > using namespace std;
    >
    > void foo(ofstream & out)
    > {
    > out << "Testing";
    >
    > }


    void foo(std::eek:stream& os)
    {
    os << "Testing";
    }

    >
    > int main()
    > {
    > ofstream file("test.txt");


    if(!file)
    {
    std::cout << "failed to open file\n";
    return 0;
    }

    > foo(file); // works
    > file.close();
    >
    > foo(cout); // DOESN'T WORK
    >
    > return 0;}
    >
    > ------------------------------------------------
    >
    > Any suggestions would be appreciated.
    > Thank you,
    > Joe Hesse
     
    Salt_Peter, Sep 26, 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. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    603
    Ivan Johansen
    Jul 14, 2005
  2. Replies:
    5
    Views:
    725
    Jim Langston
    Jan 13, 2009
  3. ksamdev
    Replies:
    3
    Views:
    414
    Stuart Redmann
    Sep 13, 2010
  4. ksamdev
    Replies:
    2
    Views:
    1,199
    ksamdev
    Sep 10, 2010
  5. Jeff Flinn
    Replies:
    0
    Views:
    397
    Jeff Flinn
    Sep 11, 2010
Loading...

Share This Page