Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Christian Schaller" <Christian.Schaller@siemens.com>
To: "Xavier Leroy" <xavier.leroy@inria.fr>
Cc: <caml-list@inria.fr>
Subject: RE: [Caml-list] closing file descriptors and channels
Date: Thu, 20 Nov 2003 17:55:40 +0100	[thread overview]
Message-ID: <A1364BC6814D92479D4EED572D6F6FD806953C@uranus.certw2k.net> (raw)

> 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          
  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 =
  let read_new_line n =
    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 =
  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


             reply	other threads:[~2003-11-20 16:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-20 16:55 Christian Schaller [this message]
2003-11-20 16:54 ` Tim Freeman
2003-11-21 16:28   ` skaller
  -- strict thread matches above, loose matches on Subject: below --
2003-11-20 16:14 Christian Schaller
2003-11-20 16:35 ` Xavier Leroy
2003-11-20 17:01   ` Tim Freeman
2003-11-20 17:14   ` Nicolas George

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=A1364BC6814D92479D4EED572D6F6FD806953C@uranus.certw2k.net \
    --to=christian.schaller@siemens.com \
    --cc=caml-list@inria.fr \
    --cc=xavier.leroy@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox