Connecting Tech Pros Worldwide Forums | Help | Site Map

redirecting streams

Michael
Guest
 
Posts: n/a
#1: Jul 22 '05
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



Russell Hanneken
Guest
 
Posts: n/a
#2: Jul 22 '05

re: redirecting streams


Michael wrote:[color=blue]
> 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)[/color]

<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
rghanneken@pobox.com
Remove the 'g' from my address to send me mail.


Jeff Schwab
Guest
 
Posts: n/a
#3: Jul 22 '05

re: redirecting streams


Michael wrote:[color=blue]
> 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)[/color]

Just use a std::ofstream (from <fstream>) instead of std::cout.
Leor Zolman
Guest
 
Posts: n/a
#4: Jul 22 '05

re: redirecting streams


On Fri, 21 May 2004 17:15:39 +0000 (UTC), "Michael"
<slick_mick_00@hotmail.com> wrote:
[color=blue]
>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
>[/color]

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
Marc
Guest
 
Posts: n/a
#5: Jul 22 '05

re: redirecting streams


"Michael" wrote:
[color=blue]
> 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)[/color]

A quick solution could be something like the exemple in:
http://www.cplusplus.com/ref/iostream/ios/rdbuf.html
Michael
Guest
 
Posts: n/a
#6: Jul 22 '05

re: redirecting streams


Thanks to you all, but it was this one that I was looking for.
Thanks

Mike

"Marc" <MarcDotGlisse@Loria.Fr> wrote in message
news:c8lmmu$14r2$1@nef.ens.fr...[color=blue]
> "Michael" wrote:
>[color=green]
> > I've written my progrma to use cout a lot, but now i want to output this[/color][/color]
to[color=blue][color=green]
> > a file instead of the screen. How do i do this Is it platform specific[/color][/color]
(if[color=blue][color=green]
> > so then sorry)[/color]
>
> A quick solution could be something like the exemple in:
> http://www.cplusplus.com/ref/iostream/ios/rdbuf.html[/color]


Frank Looper
Guest
 
Posts: n/a
#7: Jul 22 '05

re: redirecting streams


> >[color=blue][color=green]
> > A quick solution could be something like the example in:
> > http://www.cplusplus.com/ref/iostream/ios/rdbuf.html[/color]
>
>[/color]
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



Alex Vinokur
Guest
 
Posts: n/a
#8: Jul 22 '05

re: redirecting streams



"Marc" <MarcDotGlisse@Loria.Fr> wrote in message news:c8lmmu$14r2$1@nef.ens.fr...[color=blue]
> "Michael" wrote:
>[color=green]
> > 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)[/color]
>
> A quick solution could be something like the exemple in:
> http://www.cplusplus.com/ref/iostream/ios/rdbuf.html[/color]

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:alexvn@connect.to
http://mathforum.org/library/view/10978.html



jmoy
Guest
 
Posts: n/a
#9: Jul 22 '05

re: redirecting streams


"Russell Hanneken" <rghanneken@pobox.com> wrote in message news:<1krrc.3783$Tn6.2383@newsread1.news.pas.earth link.net>...[color=blue]
> Michael wrote:[color=green]
> > 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)[/color]
>
> <off-topic>[/color]

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);
red floyd
Guest
 
Posts: n/a
#10: Jul 22 '05

re: redirecting streams


naglunov@yahoo.co.in (jmoy) wrote in message news:<8db46691.0405270053.22f10940@posting.google. com>...[color=blue]
>
> 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);[/color]

So *THAT'S* how you do the equivalent of freopen() on cout! Thanks!
Closed Thread