By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

Use cout as ofstream object

P: n/a
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
Sep 26 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Sep 26 '07 #2

P: n/a
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
Sep 26 '07 #3

P: n/a
On Sep 26, 10:13 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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

Sep 26 '07 #4

P: n/a
hi ,then why can it pass the compiler?
thanks

On Sep 26, 10:12 pm, Barry <dhb2...@gmail.comwrote:
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

Sep 26 '07 #5

P: n/a
On Sep 26, 10:02 am, "Joe Hesse" <joe_he...@actcx.comwrote:
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::ostream, isn't it? Why write
an interface that is locked to std::ofstream when the same code can be
reused to accept any derivative of std::ostream, including
std::ostringstream, std::ofstream 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::ostream& 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

Sep 26 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.