From f6fd779f1cfbfa08e541c60e8f8da4c2546a6dd7 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 3 Jul 2014 20:03:32 +0200 Subject: [PATCH] fix bug in CCPrint.to_file --- core/CCPrint.ml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/core/CCPrint.ml b/core/CCPrint.ml index bcbd26db..b315b915 100644 --- a/core/CCPrint.ml +++ b/core/CCPrint.ml @@ -127,6 +127,13 @@ let fprintf oc format = buffer format +let kfprintf k oc format = + let buffer = Buffer.create 64 in + Printf.kbprintf + (fun fmt -> Buffer.output_buffer oc buffer; k fmt) + buffer + format + let printf format = fprintf stdout format let eprintf format = fprintf stderr format @@ -134,8 +141,6 @@ let _with_file_out filename f = let oc = open_out filename in begin try let x = f oc in - flush oc; - close_out oc; x with e -> close_out_noerr oc; @@ -143,7 +148,7 @@ let _with_file_out filename f = end let to_file filename format = - _with_file_out filename (fun oc -> fprintf oc format) + _with_file_out filename (fun oc -> kfprintf (fun _ -> close_out oc) oc format) (** {2 Monadic IO} *)