redirecting streams

M

Michael

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
 
R

Russell Hanneken

Michael said:
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>
 
J

Jeff Schwab

Michael said:
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::eek:fstream (from <fstream>) instead of std::cout.
 
L

Leor Zolman

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
 
F

Frank Looper

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
 
J

jmoy

Russell Hanneken said:
<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::eek:ut)){
//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);
 
R

red floyd

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::eek:ut)){
//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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top