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 RAA17646; Thu, 20 Nov 2003 17:55:43 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 RAA17604 for ; Thu, 20 Nov 2003 17:55:42 +0100 (MET) Received: from david.siemens.de (david.siemens.de [192.35.17.14]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id hAKGtf127987; Thu, 20 Nov 2003 17:55:42 +0100 (MET) Received: from mail2.siemens.de (mail2.siemens.de [139.25.208.11]) by david.siemens.de (8.11.7/8.11.7) with ESMTP id hAKGtf603052; Thu, 20 Nov 2003 17:55:41 +0100 (MET) Received: from mars.cert.siemens.de (ust.mchp.siemens.de [139.23.201.17]) by mail2.siemens.de (8.11.7/8.11.7) with ESMTP id hAKGtfm14409; Thu, 20 Nov 2003 17:55:41 +0100 (MET) Received: from uranus.cert.siemens.de (mhpa5mac.mchp.siemens.de [139.23.201.138]) by mars.cert.siemens.de (8.12.10/8.12.10/$SiemensCERT: mail/cert.mc.pre,v 1.56 2003/11/06 20:07:28 ust Exp $) with ESMTP id hAKGteBe097553; Thu, 20 Nov 2003 17:55:40 +0100 (CET) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Subject: RE: [Caml-list] closing file descriptors and channels content-class: urn:content-classes:message X-MimeOLE: Produced By Microsoft Exchange V6.0.6487.1 Date: Thu, 20 Nov 2003 17:55:40 +0100 Message-ID: Thread-Topic: [Caml-list] closing file descriptors and channels Thread-Index: AcOvhFchd9tKu7wGTTqMHyP6yUpNbAAAMhUQ From: "Christian Schaller" To: "Xavier Leroy" Cc: X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 schaller:01 schaller:01 descriptor:01 buffer:01 in-:99 descriptor:01 endline:01 type-check:01 chris:01 descriptors:01 flush:01 rec:01 underlying:01 parser:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > close_out and close_in both perform the equivalent of a Unix.close on > the underlying Unix file descriptor, and, yes, closing something that > is already closed is an I/O exception. So, one possibility is to > close only one of the three entities file_descr, oc and ic. It's best > to close the output channel oc, since this will flush its buffer. What I did was just skipping the Unix.close, but close *both* in- and out-channels and it worked (until now ;-) ). > Side remark: you'd need to close the file descriptor also in the case > where the line is found and the exception Found is raised. Yup, I've seen this one already. This one makes it complicated, since for propagating the exception, I have to duplicate the closings: . . . with =20 End_of_file -> output_string oc (line ^ "\n"); Found line -> (close_out oc; close_in ic; raise Found line); close_out oc; close_in ic Since I am new to streams & co, are there any kind of rules when to prefer streams to normal I/O? Is there more overhead when using streams? Shouldn't I use streams when working on a regular text file? What if I want to break from a stream with an exception (as above the Found exception)? Closing/accessing the channel is only possible in the stream creation as in my corrected version: let read_lines ch =3D let read_new_line n =3D try Some (input_line ch) with End_of_file -> close_in ch; None in Stream.from read_new_line Now I want to stop reading lines after a certain number of lines let rec read_n_lines n =3D parser [< 'line; rest >] -> if n <> 0 then print_endline line; read_n_lines (n-1) else () [< >] -> () Didn't type-check it, though ;-) Anyway, how can I close the stream in this case? Thanks! - Chris ------------------- 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