473,383 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Is there a way to save a copy of std::cout

Is it possible to save a copy of cout the same way you can save a copy
of stdout in C (I know C version is portable, but unix portable is
good enough for me).

I have to use a library which closes stdout on init because its used
to mostly in background processes but I need it for an interactive
process. Library cant be changed unfortunately.

I tried the following but the C++ version doesn't work, I guess
because it only shares the underlying streambuf.

#ifdef C_VERSION
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
int copy_of=dup(fileno(stdout));
fprintf(stdout, "This is before on original\n");
fclose(stdout);
fprintf(stdout, "This is after on original\n");

stdout=fdopen(copy_of, "w");

fprintf(stdout, "This is after on original after copy\n");

// Output:
// This is before on original
// This is after on original after copy

return 0;
}
#else
#include <iostream>

int main(int argc, char *argv[])
{
std::ostream copy_of(std::cout.rdbuf());

std::cout << "This is before on original\n";
copy_of << "This is before on copy\n";
fclose(stdout);
std::cout << "This is after on original\n";

copy_of << "This is after on copy\n";

// Output:
// This is before on original
// This is before on copy

return 0;
}
#endif
Aug 20 '08 #1
11 6627
Adrian wrote:
Is it possible to save a copy of cout the same way you can save a copy
of stdout in C (I know C version is portable, but unix portable is
good enough for me).
(a) What is "save a copy of cout"? Output streams are not copyable
objects, so the answer is most likely "no, unless you mean something else".

(b) "Unix portable" is an oxymoron. If it's only on one OS, it's not
"portable".

(c) If you need Unix-specific solution, you should ask in a Unix
newsgroup, don't you think?
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 20 '08 #2
On Aug 20, 1:33 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
(a) What is "save a copy of cout"? Output streams are not copyable
objects, so the answer is most likely "no, unless you mean something else".

(b) "Unix portable" is an oxymoron. If it's only on one OS, it's not
"portable".

(c) If you need Unix-specific solution, you should ask in a Unix
newsgroup, don't you think?
Good to see you are as acerbic as ever ;-)

a) Thanks so its not copyable
b) Not really - there are many flavours of unix
c) I didnt ask for one but would accept one :-)

Another question then, if the FILE * stdout has been closed/redirected
is there any way to revert std::cout back to state it had before the
closing/redirection

Or if I set stdout back to a valid FILE * will std::cout use it or is
it not defined if std::cout uses stdout.

Adrian
Aug 20 '08 #3
Adrian wrote:
[..]
Another question then, if the FILE * stdout has been closed/redirected
is there any way to revert std::cout back to state it had before the
closing/redirection
I don't see anything that would allow you to manipulate 'cout', except
making a different stringbuf and stuffing it into 'cout'.
Or if I set stdout back to a valid FILE * will std::cout use it or is
it not defined if std::cout uses stdout.
I do not know of any connection between 'std::cout' and FILE* or between
'std::cout' and 'stdout'. It is quite possible that internally 'cout'
does output to 'stdout' or they both use the same mechanism (common
denominator), but IIRC it's not mandated by the Standard. I can easily
be wrong, of course, I've not done much work with streams. Let's hope
others will chime in...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 20 '08 #4
Is it possible to save a copy of cout the same way you can save a copy
of stdout in C (I know C version is portable, but unix portable is
good enough for me).

Not directly, as you have been answered earlier. But you can create
stringstream object and output evreything you output to cout into
stringstream object too. And then you are free to do whatever you want
with your data in stringstream.
Aug 20 '08 #5
Is it possible to save a copy of cout the same way you can save a copy
of stdout in C (I know C version is portable, but unix portable is
good enough for me).
There is a way to implement something similiar:

#include <sstream>
#include <iostream>

using namespace std;

int main() {
stringstream ss;
cout << "This is a message." << endl;
ss << "This is a message." << endl;
return 0;
}
Aug 20 '08 #6
Is it possible to save a copy of cout the same way you can save a copy
of stdout in C (I know C version is portable, but unix portable is
good enough for me).
There is a way to implement something similiar:

#include <sstream>
#include <iostream>

using namespace std;

int main() {
stringstream ss;
cout << "This is a message." << endl;
ss << "This is a message." << endl;
return 0;
}
Aug 20 '08 #7
On Aug 21, 3:46*am, Adrian <n...@bluedreamer.comwrote:
Is it possible to save a copy of cout the same way you can save a copy
of stdout in C (I know C version is portable, but unix portable is
good enough for me).
- I don't know what you mean by copying stdout

However, std::ostream (the type of std::cout) has methods for getting
and setting a strambuf.

streambuf* rdbuf ( ) const;
streambuf* rdbuf ( streambuf* sb );

You can "create" a streambuf to do pretty much anything you want,
including I suspect what you're looking to do.
Aug 20 '08 #8
On Aug 20, 4:51*pm, Gianni Mariani <owebee...@gmail.comwrote:
- I don't know what you mean by copying stdout
Same as for duplicating a file descriptor
>
However, std::ostream (the type of std::cout) has methods for getting
and setting a strambuf.

streambuf* rdbuf ( ) const;
streambuf* rdbuf ( streambuf* sb );

You can "create" a streambuf to do pretty much anything you want,
including I suspect what you're looking to do.
No it isnt, how can I make the following code output everything
(without removing the fclose :-)
#include <iostream>
#include <cstdio>
#include <unistd.h>

int main(int argc, char *argv[])
{
int save_stdout=dup(fileno(stdout));
std::ostream save_cout(std::cout.rdbuf());

std::cout << "std::cout << before close\n";
fprintf(stdout, "fprintf() on stdout before close\n");

fclose(stdout);

std::cout << "std::cout << after close\n";
fprintf(stdout, "fprintf() on stdout after close\n");

stdout=fdopen(save_stdout, "w");
std::cout << "std::cout << after restore stdout\n";
fprintf(stdout, "fprintf() on stdout after restore\n");

std::cout.rdbuf(save_cout.rdbuf());
std::cout << "std::cout << after restore cout\n";
return 0;
}
Aug 21 '08 #9
Adrian schrieb:
On Aug 20, 4:51 pm, Gianni Mariani <owebee...@gmail.comwrote:
>You can "create" a streambuf to do pretty much anything you want,
including I suspect what you're looking to do.
No it isnt, how can I make the following code output everything
(without removing the fclose :-)
Yes it is.
You have to create a *new* streambuf object an let cout use it.

You can derive a class based on streambuf that uses your duplicated
stdout, or use a filebuf like this:

#include <fstream>
#include <iostream>
#include <stdio.h>

int main()
{
fclose(stdout);

std::streambuf* oldbuf = std::cout.rdbuf();
std::filebuf newbuf;
newbuf.open("somefile", std::ios::out);
std::cout.rdbuf(&newbuf);

std::cout << "Some Text" << std::endl;

// set to original streambuf
std::cout.rdbuf(oldbuf);
}

You could also output to std::cerr or fix the buggy library. :-)

--
Thomas
Aug 21 '08 #10
On Aug 21, 4:56 pm, "Thomas J. Gritzan" <phygon_antis...@gmx.de>
wrote:
Adrian schrieb:
Yes it is.
You have to create a *new* streambuf object an let cout use it.

You can derive a class based on streambuf that uses your duplicated
stdout, or use a filebuf like this:
[snip code]
Thanks, but that not quite what I needed, this outputs to a file where
as I need to output to the original place that stdout/cout does ie the
terminal.
You could also output to std::cerr or fix the buggy library. :-)
Unfortunaley its a feature of the library :-) and same lib also
redirects cerr to logs.

Just digging around the standard I found in 27.3.1.3
"The object cout controls output to a stream buffer associated with
the object stdout, declared in
<cstdio(27.8.2)."

So shouldnt this mean that if I can duplicate/save stdout then cout
should work as a fprintf(stdout) should?

Adrian
Aug 22 '08 #11
LR
Adrian wrote:
So shouldnt this mean that if I can duplicate/save stdout
I went looking through what I think is a recent version of a draft
standard for C and it suggests that stdout doesn't have to be an lvalue.
So the code that I think was posted that showed:
stdout = [don't recall what was here]
might not be portable even to various flavors of *nix.

But I'm not sure what version of the C standard the C++ standard
references and it might say something different.

LR
Aug 22 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pmb | last post by:
I've noticed a lot of people preferring std::cout over cout. May I ask why you prefer one over the other? thanks Pmb
12
by: Minti | last post by:
Is std::cout slower than printf When we call printf e.g. in printf(20 format conversion specifications, 20 arguments); Is it faster than the std::cout << { 20 redirections to the output...
6
by: clilley | last post by:
The following code causes a segmentation fault on DEC Tru64: foo.cc (built into libFoo.so) //--------------------------- include <iostream> bool createFoo() { std::cout << "createFoo" <<...
3
by: Jason Heyes | last post by:
I have a function that reads tags. The function prints any failures it encounters to std::cout. std::istream &read_tag(std::istream &is, std::string &tag) { std::string s; if (!(is >> s)) {...
6
by: nish.parikh | last post by:
Hi, I am using std::cout to print a char pointer that is NULL. Subsequent calls to std::cout dont print anything. Is this the expected behavior? example: #include <iostream> int main( int...
19
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can...
6
by: Roger | last post by:
Hello, I'm pretty new to C++ programming, and I'm teaching myself the language using various sources. This sounds stupid, but I am really confused on this... I was wondering why we have to write...
5
by: wongjoekmeu | last post by:
Dear All, I have written a small program to read in from console a user string. I wanted to be able to read in a string containing of all sorts of characters untill the user press enter. I have to...
58
by: Mark Casternoff | last post by:
I'm getting back into C++ after a long hiatus (they didn't have namespaces back then). I know this question is completely subjective, but I'd be interested in hearing which is the "better"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.