473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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
Jul 22 '05 #1
9 1354
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
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
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
"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
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
> >
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

"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jan Knop | last post by:
Hello I am writing a Windows application where I need to redirect stdin, stdout and stderr from Python. to my application Is it a simple way of do it ? Has anyone done it using Winsock ?
0
by: Christophe HELFER | last post by:
hi, I have some problem with redirecting input and output from a process. I can only use VB language (sorry...) Situation: I have to use the Cisco Network Registrar (DNS And DHCP server) ...
2
by: Max Berghammer | last post by:
Hi ! Is there any way to redirect the standard-outputstream or standard-errorstream of the currently running process ? I know that i can spawn a new process and redirect its standard-outputstream...
3
by: Rob Z | last post by:
I use popen in C++ to grab the standard output from Unix commands. Is there a similarly simple and fast way to grab standard error as well? I understand Python has different popen commands for...
2
by: Jason Heyes | last post by:
Here is what I use to redirect printed messages to a file: std::ofstream file("logfile"); std::cout.rdbuf(file.rdbuf()); std::cout << "this message goes to a file" << std::endl; Will the...
2
by: Jacek | last post by:
Hello! My application has to use external native library writing to stdout and stdin. My goal is to redirect output within running process (no chance to do that in child process - Process class...
5
by: Tim | last post by:
Hi I am running a console program through my own VB front end. I am redirecting the StandardOut and StandardInput streams to and from textboxes so that the VB front end acts like a console. My...
6
by: Christophe Helfer | last post by:
hi, I have some problem with redirecting input and output from a process. Situation: I have to use the Cisco Network Registrar (DNS And DHCP server) command line utility as redirecting its...
8
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.