From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id B168FBC8C for ; Tue, 25 Jan 2005 23:54:50 +0100 (CET) Received: from first.in-berlin.de (dialin-145-254-053-076.arcor-ip.net [145.254.53.76]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j0PMsnQO026497 for ; Tue, 25 Jan 2005 23:54:50 +0100 Received: by first.in-berlin.de (Postfix, from userid 501) id D5784A3991; Tue, 25 Jan 2005 23:54:49 +0100 (CET) Date: Tue, 25 Jan 2005 23:54:49 +0100 From: Oliver Bandel To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] open_file Message-ID: <20050125225449.GB667@first.in-berlin.de> References: <1106691570.41f6c5f250fdc@www.doc.ic.ac.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1106691570.41f6c5f250fdc@www.doc.ic.ac.uk> User-Agent: Mutt/1.5.6i X-Miltered: at concorde with ID 41F6CE39.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; oliver:01 bandel:01 oliver:01 in-berlin:01 caml-list:01 wrote:01 compilation:01 byte:01 byte:01 buffer:01 buffer:01 25,:98 ....:98 unix:01 unix:01 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.0 X-Spam-Level: On Tue, Jan 25, 2005 at 10:19:30PM +0000, yjc01@doc.ic.ac.uk wrote: > I tried to read in a text file and extract the string between (* and *) into a > variable, buff, by using the openfile and read functions in Unix module. But > get an error on compilation. > > The code I came up with is as follows: > > open Unix;; > > let file_reader = openfile "sudent.cd" [O_RDONLY] 0o640;; assuming the file "sudent.cd" exists and contains enough bytes. > let buff = ref "empty";; > > let main () = > let file_content = read file_reader !buff 1 5 in > print_int file_content; > print_string !buff;; > main (); assuming you meant main ();; in the last line,.... then you have a problem, when you read from position with index "1" (instead of "0" for the fiorst byte) and read 5 bytes into the 5 byte long string.... There is not enough space in your buffer for writing all your data into it. You may start writing at position 0 instead of 1 or only read in 4 bytes, or make your buffer larger. You may set the len of bytes to the size of bytes of your String (String.length !buff) . But you have to write from position (offset) 0. It would be better to use the try/with construct around your unix-calls; then you can handle exceptions. E.g. when there is no file "sudent.cd" then your program fails in your first line. Ciao, Oliver