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

redirecting streams

P: n/a
I've written my progrma to use cout a lot, but now i want to output this to
a file instead of the screen. How do i do this Is it platform specific (if
so then sorry)
Thanks
Mike
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Michael wrote:
I've written my progrma to use cout a lot, but now i want to output this
to a file instead of the screen. How do i do this Is it platform specific
(if so then sorry)


<off-topic>

Technically, I believe that's determined by the command shell you're using
when you run your program. But I think every command line interface I've
used has done it the same way:

myprog > outputfilename

Or, if you want to append to the file:

myprog >> outputfilename

If that doesn't work for you, or if you're not running the program from a
command line interface (maybe you're clicking a button on an IDE), you'll
probably have better luck asking on a newsgroup devoted to whatever
environment you're using.

</off-topic>

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Jul 22 '05 #2

P: n/a
Michael wrote:
I've written my progrma to use cout a lot, but now i want to output this to
a file instead of the screen. How do i do this Is it platform specific (if
so then sorry)


Just use a std::ofstream (from <fstream>) instead of std::cout.
Jul 22 '05 #3

P: n/a
On Fri, 21 May 2004 17:15:39 +0000 (UTC), "Michael"
<sl***********@hotmail.com> wrote:
I've written my progrma to use cout a lot, but now i want to output this to
a file instead of the screen. How do i do this Is it platform specific (if
so then sorry)
Thanks
Mike


If you're trying to do it from the command line without changing the
program, see Russell's answer.

If you want to modify the program to be generalized, here's an example of
implementing Jeff's technique [not that I consider his answer inadequate,
it's just that I've already written this up and this way it won't go to
waste ;-) ]. Note that all the actual output is performed by write_data(),
which is generalized in much the same way that non-member operator<<()'s
are written by convention (by taking an ostream & as a parameter):

//
// Generalizing I/O
//

#include <iostream>
#include <fstream>
#include <cctype>
#include <cstdlib>
using namespace std;

void write_data(ostream &os);

int main()
{

cout << "Output to screen (s) or file (f) ? ";

char dest;
cin >> dest;

if ((dest = std::tolower(dest)) == 's')
write_data(cout);
else if (dest == 'f')
{
ofstream of("gio_out.txt");
if (!of)
{
cout << "Couldn't create gio_out.txt. Bailing out.\n";
exit(1);
}
write_data(of);
}
else
{
cout << "Bad input. Re-run." << endl;
exit(1);
}

return 0;
}

void write_data(ostream &os)
{
os << "This is line 1\n";
os << "This is line 2\n";
os << "Etc.\n";
}
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #4

P: n/a
"Michael" wrote:
I've written my progrma to use cout a lot, but now i want to output this to
a file instead of the screen. How do i do this Is it platform specific (if
so then sorry)


A quick solution could be something like the exemple in:
http://www.cplusplus.com/ref/iostream/ios/rdbuf.html
Jul 22 '05 #5

P: n/a
Thanks to you all, but it was this one that I was looking for.
Thanks

Mike

"Marc" <Ma***********@Loria.Fr> wrote in message
news:c8***********@nef.ens.fr...
"Michael" wrote:
I've written my progrma to use cout a lot, but now i want to output this to a file instead of the screen. How do i do this Is it platform specific (if so then sorry)


A quick solution could be something like the exemple in:
http://www.cplusplus.com/ref/iostream/ios/rdbuf.html

Jul 22 '05 #6

P: n/a
> >
A quick solution could be something like the example in:
http://www.cplusplus.com/ref/iostream/ios/rdbuf.html


That was wonderfully short, and easily understood. :-)

Would you be kind enough to point to a similar method to write hex data
to a binary file?
Thank you,
Frank Looper

Jul 22 '05 #7

P: n/a

"Marc" <Ma***********@Loria.Fr> wrote in message news:c8***********@nef.ens.fr...
"Michael" wrote:
I've written my progrma to use cout a lot, but now i want to output this to
a file instead of the screen. How do i do this Is it platform specific (if
so then sorry)


A quick solution could be something like the exemple in:
http://www.cplusplus.com/ref/iostream/ios/rdbuf.html


See also
* "Redirecting cout/cerr <--> file" at http://alexvn.freeservers.com/s1/download.html
* http://groups.google.com/groups?selm...news.dfncis.de
--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #8

P: n/a
"Russell Hanneken" <rg********@pobox.com> wrote in message news:<1k*****************@newsread1.news.pas.earth link.net>...
Michael wrote:
I've written my progrma to use cout a lot, but now i want to output this
to a file instead of the screen. How do i do this Is it platform specific
(if so then sorry)


<off-topic>


One way would be to change the underlying buffer used by cout. To do
this, insert the following before cout is used:

filebuf fb;
if (!fb.open("your_file",ios_base::out)){
//Could not open file, handle error
}
filebuf *oldfb=cout.rdbuf(&fb);

If later you want to revert cout to whatever the environment had made
it point to in the beginning, you can just say:

cout.rdbuf(oldfb);
Jul 22 '05 #9

P: n/a
na******@yahoo.co.in (jmoy) wrote in message news:<8d**************************@posting.google. com>...

One way would be to change the underlying buffer used by cout. To do
this, insert the following before cout is used:

filebuf fb;
if (!fb.open("your_file",ios_base::out)){
//Could not open file, handle error
}
filebuf *oldfb=cout.rdbuf(&fb);

If later you want to revert cout to whatever the environment had made
it point to in the beginning, you can just say:

cout.rdbuf(oldfb);


So *THAT'S* how you do the equivalent of freopen() on cout! Thanks!
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.