<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/11/81eb4f0d376b6f9e176101051e0c06b2"
  from="Christian Schaller &lt;Christian.Schaller@s...&gt;"
  author="Christian Schaller"
  date="2003-11-20T16:14:15"
  subject="[Caml-list] closing file descriptors and channels"
  prev="2003/11/0e841f152ab8a939e37e43f48a0d5095"
  next="2003/11/5bd426650020fd2fdf52e8c681dca310"
  next-in-thread="2003/11/04de965f854f12208253598597db7c35"
  prev-thread="2003/11/94ca042f44be533f27f5a8b42b4b788f"
  next-thread="2003/11/5bd426650020fd2fdf52e8c681dca310"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] closing file descriptors and channels">
<msg 
  url="2003/11/81eb4f0d376b6f9e176101051e0c06b2"
  from="Christian Schaller &lt;Christian.Schaller@s...&gt;"
  author="Christian Schaller"
  date="2003-11-20T16:14:15"
  subject="[Caml-list] closing file descriptors and channels">
<msg 
  url="2003/11/04de965f854f12208253598597db7c35"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2003-11-20T16:35:41"
  subject="Re: [Caml-list] closing file descriptors and channels">
<msg 
  url="2003/11/c8c057fcc1099e7256ffb1a4b070af84"
  from="Nicolas George &lt;nicolas.george@e...&gt;"
  author="Nicolas George"
  date="2003-11-20T17:14:06"
  subject="Re: [Caml-list] closing file descriptors and channels">
</msg>
<msg 
  url="2003/11/14cf1145f2220b2d5d5aa66a8883d5d9"
  from="tim@f..."
  author="tim@f..."
  date="2003-11-20T18:04:57"
  subject="Re: [Caml-list] closing file descriptors and channels">
</msg>
</msg>
</msg>
</thread>

<contents>
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 = 
  let file_descr = Unix.openfile file_name [Unix.O_RDWR; Unix.O_APPEND]
0o644 in
  let ic = Unix.in_channel_of_descr file_descr in
  let oc = 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 
    let line = input_line ic in
    if cond line
    then raise (Found line)
  done          
  with          
  End_of_file -&gt; 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 =
  let read_new_line n =
    try Some (input_line ch)
    with End_of_file -&gt; 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

</contents>

</message>

