Connecting Tech Pros Worldwide Help | Site Map

redirecting streams

  #1  
Old July 22nd, 2005, 12:24 PM
Michael
Guest
 
Posts: 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


  #2  
Old July 22nd, 2005, 12:24 PM
Russell Hanneken
Guest
 
Posts: n/a

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.


  #3  
Old July 22nd, 2005, 12:24 PM
Jeff Schwab
Guest
 
Posts: n/a

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, 12:24 PM
Leor Zolman
Guest
 
Posts: n/a

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, 12:25 PM
Marc
Guest
 
Posts: n/a

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, 12:25 PM
Michael
Guest
 
Posts: n/a

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, 12:26 PM
Frank Looper
Guest
 
Posts: n/a

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, 12:26 PM
Alex Vinokur
Guest
 
Posts: n/a

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, 12:34 PM
jmoy
Guest
 
Posts: n/a

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);
  #10  
Old July 22nd, 2005, 12:35 PM
red floyd
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extend streams in C++ Ioannis Papadopoulos answers 6 January 14th, 2008 11:25 AM
Redirecting STDIN and STDOUT Christophe HELFER answers 0 November 22nd, 2005 11:15 AM
redirecting STDIN/STDOUT Christophe Helfer answers 6 November 20th, 2005 07:13 PM
Redirecting STDIN and STDOUT Christophe HELFER answers 0 July 21st, 2005 03:29 PM