caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Robert Longeon <Robert.Longeon@cnrs-dir.fr>
Cc: caml-list@inria.fr
Subject: [Caml-list] Re: [Caml-list] Comment rediriger les entrées/sorties  standards ?
Date: Thu, 3 May 2001 11:51:44 +0200	[thread overview]
Message-ID: <20010503115144.C25957@pauillac.inria.fr> (raw)
In-Reply-To: <4.2.0.58.20010502143518.00da89a0@cezanne.auteuil.cnrs-dir.fr>; from Robert.Longeon@cnrs-dir.fr on Wed, May 02, 2001 at 02:42:06PM +0200

[English summary: redirecting standard output to a file from a
 Caml program; Unix file descriptor hacking.]

> Je voudrait rediriger les entrées/sorties standards. Je croyais pouvoir le 
> faire en tapant :
> let std_out = open_out "c:\projet\entree"
> mais  l'écriture se fait toujours à l'écran.

Vous pouvez facilement rediriger la sortie du formatteur (module
Format, la chose qui imprime joliment les sorties du toplevel en
particulier) à l'aide de Format.set_formatter_out_channel.

Pour rediriger toutes les sorties faites sur stdout, il faut se
plonger dans les arcanes du module Unix:

  let oc = 
    Unix.openfile "/tmp/myfile"
                  [Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC] 0o666 in
  flush stdout;
  Unix.dup2 oc Unix.stdout;
  Unix.close oc

Après exécution du code ci-dessus, toutes les sorties sur stdout
(et même celles sur Unix.stdout) iront dans le fichier /tmp/myfile.
Attention, ceci n'est pas réversible.  Si plus tard vous voulez
annuler la redirection, il faut faire un peu plus compliqué:

  let saved_stdout = Unix.dup Unix.stdout in
  let oc = 
    Unix.openfile "/tmp/myfile"
                  [Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC] 0o666 in
  flush stdout;
  Unix.dup2 oc Unix.stdout;
  Unix.close oc;
  (* ici toutes les sorties sur stdout vont dans le fichier *)
  flush stdout;
  Unix.dup2 saved_stdout Unix.stdout;
  Unix.close saved_stdout;
  (* ici on est revenu à l'état initial *)

Ces exemples sont pour Objective Caml, mais je crois que l'on peut
faire la même chose en Caml Light, mutando mutandis.

- Xavier Leroy
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


      parent reply	other threads:[~2001-05-03  9:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-05-02 12:42 Robert Longeon
2001-05-02 14:49 ` [Caml-list] =?iso-8859-1?Q?Comment_rediriger_les_entrées/sorties_?= " Maxence Guesdon
     [not found] ` <200105021455.f42EtjL25069@waco.inria.fr>
2001-05-02 15:20   ` [Caml-list] Comment_rediriger_les_entrees/sorties_standards ? Robert Longeon
2001-05-03  6:21 ` [Caml-list] Comment rediriger les entrées/sorties standards ? Bruno Pagano
2001-05-03  9:51 ` Xavier Leroy [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20010503115144.C25957@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=Robert.Longeon@cnrs-dir.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).