caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Brown <caml-list@davidb.org>
To: Oleg <oleg_inconnu@myrealbox.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Format question
Date: Wed, 6 Nov 2002 15:46:26 -0800	[thread overview]
Message-ID: <20021106234626.GA11254@opus.davidb.org> (raw)
In-Reply-To: <200211062249.RAA07810@apakabar.cc.columbia.edu>

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


  reply	other threads:[~2002-11-06 23:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-06 22:49 Oleg
2002-11-06 23:46 ` David Brown [this message]
2002-11-20  0:29 ` David Frese
2002-11-07  0:09 Oleg
2002-11-07  0:31 ` Alessandro Baretta
2002-11-07  0:46 ` Gerd Stolpmann

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=20021106234626.GA11254@opus.davidb.org \
    --to=caml-list@davidb.org \
    --cc=caml-list@inria.fr \
    --cc=oleg_inconnu@myrealbox.com \
    /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).