help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

dup2() on stdout [or using disp() on another file stream]


From: John W. Eaton
Subject: dup2() on stdout [or using disp() on another file stream]
Date: Thu, 21 Oct 1999 02:43:39 -0500 (CDT)

On 16-Oct-1999, Joao Cardoso <address@hidden> wrote:

| How can I use 'disp' on a file stream other than 'stdout'?
| 
| 'disp' is an easy and nice way to print matrices, without the need to
| create a format string to use on 'fprintf'. I would like to:
| 
|      f_name = tmpnam
|      fout = fopen(f_name,"w");
|      disp(fout, rand(3))        <--- not supporred
|      fclose(fout)
| 
| When I tried to use:
| 
|     f_name = tmpnam
|     fout = fopen(f_name,"w");
|     old = fopen("old","w");
| 
|     dup2(stdout, old); # save old stdout on 'old'?
|     dup2(fout, stdout); # use new stdout
| 
|     disp(rand(3))
| 
|     dup2(old, stdout);  #revert to saved stdout?
| 
| I wasn't able to revert stdout to the original stdout. what is wrong? I
| can't 'fclose(stdout)', either.

What version of Octave are you using?

FWIW, it works for me with 2.0.14 and also with the current
bleeding-edge sources on a Linux system.

Unfortunately, after you put stdout on fout, you also get the prompt
and the commands in the output file.  (You might as well use diary!)

Perhaps it would be better to just implement an fdisp() function that
does what you want?  Or, in the current bleeding-edge sources, Octave
has a `file' data type and a C++-like output operator, so you can
write:

  file = fopen (fname, "w");
  file << rand (3);

to print a formatted matrix to a file.

(I'm not sure that the `<<' operator is a good idea though -- this feature is
still experimental.)

jwe



---------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.  To ensure
that development continues, see www.che.wisc.edu/octave/giftform.html
Instructions for unsubscribing: www.che.wisc.edu/octave/archive.html
---------------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]