From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA10727 for caml-redistribution; Fri, 20 Nov 1998 09:40:18 +0100 (MET) 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 JAA22091 for ; Fri, 20 Nov 1998 09:36:38 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id JAA19852; Fri, 20 Nov 1998 09:36:35 +0100 (MET) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA18006; Fri, 20 Nov 1998 09:36:15 +0100 (MET) From: Pierre Weis Message-Id: <199811200836.JAA18006@pauillac.inria.fr> Subject: Re: Binary file I/O To: whitley@cse.buffalo.edu (John Whitley) Date: Fri, 20 Nov 1998 09:36:15 +0100 (MET) Cc: caml-list@inria.fr In-Reply-To: <13907.9032.867059.29471@pollux.cs.Buffalo.EDU> from "John Whitley" at Nov 20, 98 00:07:07 am X-Mailer: ELM [version 2.4 PL24 ME8] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: weis > Is there a standard way of handling binary file I/O in OCaml, or must > I resort to handling input and output bitstream formatting in C? > > Thanks, > John Whitley The standard way to input and output binary files in Caml is to use the bin versions of the channel opening primitives open_in and open_out, namely open_in_bin and open_out_bin (those primitives prevent the interpretation and convertion of end-of-line characters). Then you read/write the channel as usual, either one character at a time using input_char/output_char or directly via a string buffer of your own using the input/output primitives. Direct handling of binary integers is also available using input_binary_int/output_binary_int. In any case, you may access to the bits of your bitstream by first accessing the characters and then applying the usual mask and shift stuff (lnot) (land) (lor) (lsr) (lsl). For easier handling of these operations you may define associated infix symbols, for instance, trying to be (loosely) reminiscent of C syntax: let ( << ) = (lsl) and ( >> ) = (lsr) and ( &! ) = (land) and ( |! ) = (lor) and ( ~! ) = (lnot);; Then proceed as usual, for instance let nth_bit n j = (n &! (0x1 << j)) >> j;; let nth_bit_char c j = nth_bit (Char.code c) j;; Best regards, Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/