473,547 Members | 2,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use cout as ofstream object

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
5 11374
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
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
On Sep 26, 10:13 pm, "Victor Bazarov" <v.Abaza...@com Acast.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
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
On Sep 26, 10:02 am, "Joe Hesse" <joe_he...@actc x.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::ostringstr eam, 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::ostrea m& 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6244
by: uli | last post by:
Hi all! I'm posting to both newsgroups, because it's actually a C++ problem but could be that some of you using Matlab-&-MEX-&-C++ was struggling with the same problem. I'm trying to rewrite some Matlab routines in C++ for reusing them identically in Matlab and some other simulation tools. For computational algebra I want to use the Matrix...
9
2110
by: Ingo Nolden | last post by:
Hi there, I am writing c++ for some months now. I think I know the language now, but not yet all the tricky things of stl. For a kernel which is not using mfc I am writing a serialization. For some reasons I want to implement it on my own. One goal is to make it robust against version changes and corrupted files. That means, when an object...
6
21049
by: Omid | last post by:
Hi. I have problems when I try to redirect everything that is sent to cout to a file. I have one piece of code that works and one that does not work. The only difference is which headers I use. What the code does: * First writes "This is sent to prompt" to prompt.
5
18529
by: Squid Seven | last post by:
I'm trying to use a pointer to an ofstream object and having problems: ofstream *sessionFile = NULL; if( directory == "" ) sessionFile = new ofstream( fileName.c_str(), ios::out ); else {
2
2217
by: bob | last post by:
Hi, I'm working with an ofstream object and using it to print the contents on an 100 * 100 matrix. i.e. lots of elements. the _matrix variable below is defined as std::vector<std::string, std::string> When I run the code below I see the TTK_LOG( our logging function) call correctly outputting all the values in the matrix. However I never...
4
2253
by: blackswift | last post by:
Hello,all I hava a problem that when did cout call its destructor? I used GCC compiler under linux , It compiles OK. and gives me : cons des as I haved expected.
5
4619
by: Cliff Martin | last post by:
Hi, I am writing a simple filter type program that can take input from file or stdin, and output to a file or stdout. It would make life much easier if I could assign cin to a ifstream object and read it like a file and do the same for cout (to an ofstream). Any ideas how I could do this? Or perhaps some comments on how I could redo the...
23
4833
by: mahesh | last post by:
Hi all, I have following code that is supposed to increase the power by specified value. int main() { system("cls"); int i, exponent; double base; double new_base=0.0;
2
2863
by: pompom | last post by:
hello all! i have to make a project for school , making a newsserver and a client. the thing is, i send messages between them to let the user know if everything is running ok. just some simple http messages as 201, etc. as a extention id like to build in a log file maker. what i want is: everything that is put on the output stream will...
0
7510
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7463
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3493
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3473
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1923
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1050
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
748
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.