Connecting Tech Pros Worldwide Help | Site Map

redirecting streams

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 11:24 AM
Michael
Guest
 
Posts: n/a
Default redirecting streams

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



  #2  
Old July 22nd, 2005, 11:24 AM
Russell Hanneken
Guest
 
Posts: n/a
Default Re: [OT] 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.


  #3  
Old July 22nd, 2005, 11:24 AM
Jeff Schwab
Guest
 
Posts: n/a
Default 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.
  #4  
Old July 22nd, 2005, 11:24 AM
Leor Zolman
Guest
 
Posts: n/a
Default 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
  #5  
Old July 22nd, 2005, 11:25 AM
Marc
Guest
 
Posts: n/a
Default 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
  #6  
Old July 22nd, 2005, 11:25 AM
Michael
Guest
 
Posts: n/a
Default 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]


  #7  
Old July 22nd, 2005, 11:26 AM
Frank Looper
Guest
 
Posts: n/a
Default 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



  #8  
Old July 22nd, 2005, 11:26 AM
Alex Vinokur
Guest
 
Posts: n/a
Default 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



  #9  
Old July 22nd, 2005, 11:34 AM
jmoy
Guest
 
Posts: n/a
Default Re: [OT] 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);
  #10  
Old July 22nd, 2005, 11:35 AM
red floyd
Guest
 
Posts: n/a
Default Re: [OT] 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!
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.