473,508 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

redirecting stdout to ostream

Hi,

The short story: I have to redirect stdout (not cout!!) to an ostream.
How can I manage this?

The long story:
I use the NAGC numerics library with C++ and want to use its output
routines for e.g. complex matrices. The problem: You can either print
to a file (given by filename) or to stdout. To call the NAGC-method
within an operator<< thus I have to redirect stdout to the given
ostream. But how can I initialize an ostream from a FILE *?

regards,
alex
Jul 22 '05 #1
4 6293
"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote...
The short story: I have to redirect stdout (not cout!!) to an ostream.
How can I manage this?

The long story:
I use the NAGC numerics library with C++ and want to use its output
routines for e.g. complex matrices. The problem: You can either print
to a file (given by filename) or to stdout. To call the NAGC-method
within an operator<< thus I have to redirect stdout to the given
ostream. But how can I initialize an ostream from a FILE *?


It's not a good idea to use a numerics library that has some output
(beyond reporting terminal conditions). Contact the makers of the
library and ask them to give you an option to define where the output
goes. Better yet, make them remove all output from the _numerics_
library and leave only the calculations in.

Just MHO.
Jul 22 '05 #2
You misunderstood my intention. NAGC has very nice and
customizable routines for printing matrices. Think of large
matrices with hundreds of columns. You have to split rows into sectors,
you have to determine the width of each element and so on to get a version
readable on a normal terminal.
So I do not want to this work on my own. But the library, which is
written in plain C offers two targets for printing only. A file (given by
filename) or stdout.
And now I want to redirect this given stdout to an ostream. because I
want to provide something like

std::ostream &
operator<<(std::ostream &out, const ComplexMatrix &C)
{
--> redirect stdout to out.
// here NAGC does all the work.
return out;
}

I hope now it's clear. Short again: I have to redirect the 'C-style' stdout
to a 'C++-style' std::ostream. Is there a possibility?

alex

P.S.: I consider it absolutely hopeless to have NAGC modify its interface.
Jul 22 '05 #3
"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote...
You misunderstood my intention. NAGC has very nice and
customizable routines for printing matrices. Think of large
matrices with hundreds of columns. You have to split rows into sectors,
you have to determine the width of each element and so on to get a version
readable on a normal terminal.
So I do not want to this work on my own. But the library, which is
written in plain C offers two targets for printing only. A file (given by
filename) or stdout.
And now I want to redirect this given stdout to an ostream. because I
want to provide something like

std::ostream &
operator<<(std::ostream &out, const ComplexMatrix &C)
{
--> redirect stdout to out.
// here NAGC does all the work.
return out;
}

I hope now it's clear. Short again: I have to redirect the 'C-style' stdout to a 'C++-style' std::ostream. Is there a possibility?
Of course there is a possibility. Reopen stdout as a temporary file, let
them write the matrix there, read it and then output it where you need it.
That's the simplest way I know.

You don't have to reopen stdout, though, since they provide output to
a file. Just slip them your temporary file and once they've done with
it, read it and output where needed.

You could try working around FILE*/stream incompatibility by providing
your own stream buffers, and I am sure somebody has done that before.
Try googling for something like "open fstream fopen".

alex

P.S.: I consider it absolutely hopeless to have NAGC modify its interface.


What do they consider? Have you asked?
Jul 22 '05 #4
Victor Bazarov wrote:

Of course there is a possibility. Reopen stdout as a temporary file, let
them write the matrix there, read it and then output it where you need it.
That's the simplest way I know.

You don't have to reopen stdout, though, since they provide output to
a file. Just slip them your temporary file and once they've done with
it, read it and output where needed.

That is of course possible. But is it reasonable to open a file if you just
want to print to screen? You have to have write permissions to do so (OK,
you
could take /tmp), but our program has to be portable even for windows.
It would be enough to redirect stdout/stderr to their counterpart.
You could try working around FILE*/stream incompatibility by providing
your own stream buffers, and I am sure somebody has done that before.
Try googling for something like "open fstream fopen".


That's much overhead. Too much for me (and a little of topic for our
library).

regards,
alex
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
12690
by: Birch | last post by:
I have a python script that uses the print function throughout, and as well uses calls to os.system() to spawn DOS commandline executables. My issue is that I redirect all of the output from this...
3
3736
by: David Douard | last post by:
Hi everybody, let me explain by problem: I am working on an application which consists in a C++ dll (numeric computations) and a Python IHM (Python/Tk), which must run under Linux and win32. My...
3
3411
by: Laszlo Zsolt Nagy | last post by:
Hello, I have this code: s = smtplib.SMTP() s.set_debuglevel(1) s.connect(host=smtp_host) s.set_debuglevel(0) log("Connected, sending e-mail") sys.stdout.flush()
15
3070
by: Matt Burland | last post by:
I'm having a problem with redirecting the StandardOutput of a process that I use to run a DOS program in my application. The problem is that I when I start my process (which I do in a separate...
9
1752
by: Fuzzyman | last post by:
Hello, I'm trying to redirect standard out in a single namespace. I can replace sys.stdout with a custom object - but that affects all namespaces. There will be code running simultaneously...
8
5763
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...
1
2569
by: pp4175 | last post by:
Hello Everyone, I am currently working on writing a GUI wrapper for a Menu driven DOS Program. What I was looking on doing is redirecting stdout/stdin to a stream and read/write similar to a...
5
12027
by: twiggy182 | last post by:
Hi, I really need you help because I'm not very familliar with ASP and I could not find any solution to my problem. To put you in situation, I have a CGI to which I send a file name, and that...
6
2929
by: drhilbert | last post by:
Dear all, in these days, we have found a problem we can't solve even after long long googling, so we are here asking your precious help. In our program instead of using cout to print messages...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.