redirecting stdout to ostream

  • Thread starter Alexander Stippler
  • Start date
A

Alexander Stippler

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
 
V

Victor Bazarov

Alexander Stippler said:
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.
 
A

Alexander Stippler

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::eek:stream &
operator<<(std::eek:stream &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::eek:stream. Is there a possibility?

alex

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

Victor Bazarov

Alexander Stippler said:
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::eek:stream &
operator<<(std::eek:stream &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::eek:stream. 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?
 
A

Alexander Stippler

Victor said:
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
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top