<?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/5e21cd78e94318bd8f496dda39b97d60"
  from="Christian Schaller &lt;Christian.Schaller@s...&gt;"
  author="Christian Schaller"
  date="2003-11-20T16:55:43"
  subject="RE: [Caml-list] closing file descriptors and channels"
  prev="2003/11/c3fd41ebe10e477e7c35b3f0f7cefdce"
  next="2003/11/23a879998cfddd8731a76b4f7972603e"
  next-in-thread="2003/11/e91ee45628b74c0778bbf6e062e9de53"
  prev-thread="2003/11/5bd426650020fd2fdf52e8c681dca310"
  next-thread="2003/11/ac68477a0da5d7dbb8ba9e0bbe74e550"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="RE: [Caml-list] closing file descriptors and channels">
<msg 
  url="2003/11/5e21cd78e94318bd8f496dda39b97d60"
  from="Christian Schaller &lt;Christian.Schaller@s...&gt;"
  author="Christian Schaller"
  date="2003-11-20T16:55:43"
  subject="RE: [Caml-list] closing file descriptors and channels">
<msg 
  url="2003/11/e91ee45628b74c0778bbf6e062e9de53"
  from="tim@f..."
  author="tim@f..."
  date="2003-11-20T18:00:14"
  subject="Re: [Caml-list] closing file descriptors and channels">
<msg 
  url="2003/11/dea488310b571a08836a76b61cb663d6"
  from="skaller &lt;skaller@o...&gt;"
  author="skaller"
  date="2003-11-21T17:29:23"
  subject="Re: [Caml-list] closing file descriptors and channels">
</msg>
</msg>
</msg>
</thread>

<contents>
&gt; close_out and close_in both perform the equivalent of a Unix.close on
&gt; the underlying Unix file descriptor, and, yes, closing something that
&gt; is already closed is an I/O exception.  So, one possibility is to
&gt; close only one of the three entities file_descr, oc and ic.  It's best
&gt; 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 ;-) ).

&gt; Side remark: you'd need to close the file descriptor also in the case
&gt; 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          
  End_of_file -&gt; output_string oc (line ^ "\n");
  Found line -&gt; (close_out oc; close_in ic; raise Found line);
  close_out oc;
  close_in ic

Since I am new to streams &amp; 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 =
  let read_new_line n =
    try Some (input_line ch)
    with End_of_file -&gt; 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 =
  parser
  [&lt; 'line; rest &gt;] -&gt; if n &lt;&gt; 0 then print_endline line; read_n_lines
(n-1) else ()
  [&lt; &gt;] -&gt; ()

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

</contents>

</message>

