caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: c-job <c-job@dassault-data-services.fr>, caml-list@inria.fr
Subject: Re: Stdout
Date: Mon, 17 Jan 2000 17:34:43 +0100	[thread overview]
Message-ID: <20000117173443.36428@pauillac.inria.fr> (raw)
In-Reply-To: <387C4E6C.B3319452@dassault-data-services.fr>; from c-job on Wed, Jan 12, 2000 at 09:50:36AM +0000

[Summary: Caml equivalent of freopen() in ANSI C? ]

> Je cherche a detourner l'association des flots de texte stdout de son
> peripherique habituel vers un fichier.
> La fonction freopen permet cela en C.
> Existe t il une telle fonction en Caml, s agit il de la fonction :
> val descr_of_out_channel : out_channel -> file_descr ?

Vous êtes sur la bonne voie.  On peut en effet émuler le freopen de C
à l'aide de la fonction Unix.dup2 d'OCaml.  Voici un exemple:

let reopen_out outchan filename =
  flush outchan;
  let fd1 = Unix.descr_of_out_channel outchan in
  let fd2 =
    Unix.openfile filename [Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC] 0o666 in
  Unix.dup2 fd2 fd1;
  Unix.close fd2

let _ =
  print_string "Some text on stdout\n";
  reopen_out stdout "/tmp/foo";
  print_string "Some text on /tmp/foo\n"

Ceci dit, il est souvent plus souple d'écrire son code Caml de manière
à ce qu'il prenne le canal de sortie en argument, ou le trouve dans
une référence globale:

let current_output = ref stdout

let myfunction () =
  ... output_string !current_output s ...

- Xavier Leroy




      reply	other threads:[~2000-01-17 16:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-01-12  9:50 Stdout c-job
2000-01-17 16:34 ` 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=20000117173443.36428@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=c-job@dassault-data-services.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).