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 RAA15003; Thu, 20 Nov 2003 17:14:15 +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 RAA15125 for ; Thu, 20 Nov 2003 17:14:14 +0100 (MET) Received: from goliath.siemens.de (goliath.siemens.de [192.35.17.28]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id hAKGED123314 for ; Thu, 20 Nov 2003 17:14:13 +0100 (MET) Received: from mail1.siemens.de (mail1.siemens.de [139.23.33.14]) by goliath.siemens.de (8.11.7/8.11.7) with ESMTP id hAKGECV07181 for ; Thu, 20 Nov 2003 17:14:12 +0100 (MET) Received: from mars.cert.siemens.de (ust.mchp.siemens.de [139.23.201.17]) by mail1.siemens.de (8.11.7/8.11.7) with ESMTP id hAKGEC926434 for ; Thu, 20 Nov 2003 17:14:12 +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 hAKGECBe040968 for ; Thu, 20 Nov 2003 17:14:12 +0100 (CET) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Subject: [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:14:11 +0100 Message-ID: Thread-Topic: closing file descriptors and channels Thread-Index: AcOvgVWoOaG48jaqQJKPf9iobdRCkg== From: "Christian Schaller" To: X-Loop: caml-list@inria.fr X-Spam: no; 0.00; schaller:01 schaller:01 descriptor:01 openfile:01 rdwr:01 descriptor:01 butt:99 chris:01 ocaml:01 descriptors:01 exception:02 unix:02 unix:02 string:03 sys:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi, Can anyone tell me how to close a file descriptor successfully? In the code below (* Append new line if condition cond is not satisfied on any line. Otherwise, raise exception and return the line that fulfills the condition. *) let append_line_cond file_name line cond =3D=20 let file_descr =3D Unix.openfile file_name [Unix.O_RDWR; = Unix.O_APPEND] 0o644 in let ic =3D Unix.in_channel_of_descr file_descr in let oc =3D Unix.out_channel_of_descr file_descr in try (* Read line by line and check if condition is true. If so, leave the function and return the line that satisfied the condition *) while true do=20 let line =3D input_line ic in if cond line then raise (Found line) done =20 with =20 End_of_file -> output_string oc (line ^ "\n"); (* close all channels *) Unix.close file_descr; close_out oc; close_in ic I get a Sys_error "Bad file descriptor". As far as I figured out, the problem is related with the creation of the two channels for input and output. If I comment out the Unix.close, everything works fine, but I don't know if the file_descr will be automatically closed. Hm... Yet another closing-channels-question. If I use streams instead of reading a file line by line, where exactly do I have to close the channel? Is close_in on the right position below? let read_lines ch =3D let read_new_line n =3D try Some (input_line ch) with End_of_file -> close_in ch in Stream.from read_new_line Thank you so much, but working with files in OCaml is kind of pain in the butt :( Cheers, 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