From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA10182 for caml-redistribution; Mon, 17 Jan 2000 17:55:16 +0100 (MET) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA20151 for ; Mon, 17 Jan 2000 17:35:34 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id RAA09940; Mon, 17 Jan 2000 17:34:44 +0100 (MET) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA12833; Mon, 17 Jan 2000 17:34:43 +0100 (MET) Message-ID: <20000117173443.36428@pauillac.inria.fr> Date: Mon, 17 Jan 2000 17:34:43 +0100 From: Xavier Leroy To: c-job , caml-list@inria.fr Subject: Re: Stdout References: <387C4E6C.B3319452@dassault-data-services.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Mailer: Mutt 0.89.1 In-Reply-To: <387C4E6C.B3319452@dassault-data-services.fr>; from c-job on Wed, Jan 12, 2000 at 09:50:36AM +0000 Sender: weis [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