Loading ...
Sorry, an error occurred while loading the content.

Re: "ocaml_beginners"::[] file descriptor position

Expand Messages
  • Richard Jones
    ... Just as in C you cannot mix access to buffered and unbuffered files. Rich. -- Richard Jones Red Hat
    Message 1 of 4 , Sep 6 3:26 AM
    • 0 Attachment
      On Thu, Sep 04, 2008 at 10:13:41PM +0200, Florent Monnier wrote:
      > let ic = Unix.in_channel_of_descr fd in

      Just as in C you cannot mix access to buffered and unbuffered files.

      Rich.

      --
      Richard Jones
      Red Hat
    • Florent Monnier
      Hi, ... Well, I don t own yet those tricks of buffered or unbuffered channels in OCaml. It doesn t seems to appear in the OCaml documentation. Here is my
      Message 2 of 4 , Sep 7 2:31 PM
      • 0 Attachment
        Hi,

        > > let ic = Unix.in_channel_of_descr fd in
        >
        > Just as in C you cannot mix access to buffered and unbuffered files.

        Well, I don't own yet those tricks of buffered or unbuffered channels in
        OCaml. It doesn't seems to appear in the OCaml documentation.

        Here is my current task, I have a file in which I have to reorder the
        contents, so I nead a read/write mode. The Unix module only provide one read
        and write function, while the Pervasives provides a lot of facility functions
        to read and write, but not a read/write mode, so here is the first work
        around I've started to use:

        type io = {
        ic: in_channel;
        oc: out_channel;
        }

        let open_file name =
        let fd = Unix.openfile name [Unix.O_RDWR; Unix.O_CREAT] 0o600 in
        { ic = Unix.in_channel_of_descr fd;
        oc = Unix.out_channel_of_descr fd;
        }

        but it just don't work!
        Even if you put a (flush io.oc;), when you read back a modified part through
        io.ic you get the content of this part before it was modified.
        So Pervasives.flush doesn't seems to flush a lot :)

        Now, here is the workaround I've found:

        type io = {
        fd: Unix.file_descr;
        mutable ic: in_channel;
        oc: out_channel;
        }

        let open_file name =
        let fd = Unix.openfile name [Unix.O_RDWR; Unix.O_CREAT] 0o600 in
        { fd = fd;
        ic = Unix.in_channel_of_descr fd;
        oc = Unix.out_channel_of_descr fd;
        }

        (* then everywhere I need to flush the output, I use: *)
        flush io.oc;
        io.ic <- Unix.in_channel_of_descr io.fd;


        Probably that it's very dirty so if you know a better way please let me know!

        --
        Florent
      • Richard Jones
        ... The thing is this isn t really an OCaml issue at all. You probably want to grab a book about Unix programming, such as the classics like Advanced
        Message 3 of 4 , Sep 7 2:43 PM
        • 0 Attachment
          On Sun, Sep 07, 2008 at 11:31:00PM +0200, Florent Monnier wrote:
          > Hi,
          >
          > > > let ic = Unix.in_channel_of_descr fd in
          > >
          > > Just as in C you cannot mix access to buffered and unbuffered files.
          >
          > Well, I don't own yet those tricks of buffered or unbuffered channels in
          > OCaml. It doesn't seems to appear in the OCaml documentation.

          The thing is this isn't really an OCaml issue at all. You probably
          want to grab a book about Unix programming, such as the classics like
          'Advanced Programming in the UNIX Environment', 'TCP/IP Illustrated'
          or 'UNIX Network Programming', all by W. Richard Stevens
          (http://en.wikipedia.org/wiki/W._Richard_Stevens). Or 'Practical UNIX
          Programming' by Kay/Robbins.

          Rich.

          --
          Richard Jones
          Red Hat
        Your message has been successfully submitted and would be delivered to recipients shortly.