From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id AAA31878; Thu, 7 Nov 2002 00:46:30 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA32356 for ; Thu, 7 Nov 2002 00:46:29 +0100 (MET) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from opus.davidb.org (24-165-4-233.san.rr.com [24.165.4.233]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id gA6NkSD29669 for ; Thu, 7 Nov 2002 00:46:28 +0100 (MET) Received: from davidb by opus.davidb.org with local (Exim 3.31 #1 (Debian)) id 189ZsQ-0002wL-00; Wed, 06 Nov 2002 15:46:26 -0800 Date: Wed, 6 Nov 2002 15:46:26 -0800 From: David Brown To: Oleg Cc: caml-list@inria.fr Subject: Re: [Caml-list] Format question Message-ID: <20021106234626.GA11254@opus.davidb.org> References: <200211062249.RAA07810@apakabar.cc.columbia.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200211062249.RAA07810@apakabar.cc.columbia.edu> User-Agent: Mutt/1.4i Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wed, Nov 06, 2002 at 05:49:41PM -0500, Oleg wrote: > Is it possible to define a printf that automatically flushes stdout? > > let my_printf x = Printf.printf x; flush stdout;; > > my_printf "%i" 3;; The problem is that printf takes multiple arguments, or at least appears to. You could try: let my_printf x = let res = Printf.printf x in flush stdout; res which would be correctly typed, but the flush would happen right before the first format string. Really, you would have to grab the definition of printf from the source, and change it to do what you want. This makes it a lot less portable. let my_fprintf chan fmt = let fmt = (Obj.magic fmt : string) in let len = String.length fmt in let rec doprn i = if i >= len then begin flush chan; Obj.magic () end else match String.unsafe_get fmt i with | '%' -> Printf.scan_format fmt i cont_s cont_a cont_t | c -> output_char chan c; doprn (succ i) and cont_s s i = output_string chan s; doprn i and cont_a printer arg i = printer chan arg; doprn i and cont_t printer i = printer chan; doprn i in doprn 0 let my_printf fmt = my_fprintf stdout fmt ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners