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 17B3FBB91 for ; Wed, 26 Jan 2005 15:41:45 +0100 (CET) Received: from finch.doc.ic.ac.uk (finch.doc.ic.ac.uk [146.169.1.194]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j0QEfi2J015677 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 26 Jan 2005 15:41:44 +0100 Received: from quantum06.doc.ic.ac.uk ([146.169.50.156]) by finch.doc.ic.ac.uk with esmtps (TLSv1:AES256-SHA:256) (Exim 4.43) id 1CtoMX-0001Mh-DP; Wed, 26 Jan 2005 14:41:41 +0000 Date: Wed, 26 Jan 2005 14:41:41 +0000 (GMT) From: Yan Jun Daisy Chen To: Karl Zilles Cc: caml-list@yquem.inria.fr Subject: [Caml-list] read file In-Reply-To: <41F6CB7B.7050406@1969.ws> Message-ID: References: <1106691570.41f6c5f250fdc@www.doc.ic.ac.uk> <41F6CB7B.7050406@1969.ws> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Miltered: at concorde with ID 41F7AC28.003 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 fstat:01 rec:01 wrote:01 wrote:01 compilation:01 ocamlc:01 ocamlopt:01 cmxa:01 overkill:01 pervasives:01 ocaml:01 ocaml:01 beginners:01 daisy:98 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.5 required=5.0 tests=FROM_ENDS_IN_NUMS autolearn=disabled version=3.0.0 X-Spam-Level: I am trying to extract all the comments i.e. all the text between (* and *) in a text file and store them in a list of string. How do u determin the length of the file? My idea is to read in the file_descr into a string variable, buff. Then extract the comments recursively from buff. What would be the appropriate string functions to do so? Or is there a simpler way to read the comments in directly from file? The code I come up with is as folllows: open Unix;; let fileReader = openfile "student.cd" [O_RDONLY] 0o640;; let buff = ref "file content: ";; (*let fileSize = (fstat fileReader).st_size;;*) let fileSize = 10000000;; let noOfChar = ref 0;; let rec extract_comment () = let openIndex = 0 in noOfChar := read fileReader !buff openIndex fileSize;; let main () = (*let fileContent = read fileReader !buff 0 5 in print_int fileContent;*) print_int !noOfChar;; print_newline();; print_string !buff;; print_newline();; main ();; Thanks Daisy On Tue, 25 Jan 2005, Karl Zilles wrote: >(forgot to copy the list, originally) > >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. > > >My guess is that you aren't including the unix library on your compile >command line: > > ocamlc [other_options] unix.cma [other_files] > ocamlopt [other_options] unix.cmxa [other_files] > > > > > The code I came up with is as follows: > > open Unix;; > > > > let file_reader = openfile "sudent.cd" [O_RDONLY] 0o640;; > > let buff = ref "empty";; > > > > let main () = > > let file_content = read file_reader !buff 1 5 in > > >Strings are indexed from 0, so change above to >let file_content = read file_reader !buff 0 5 in > > > > print_int file_content; > > print_string !buff;; > > main (); > > I couldn't work out what went wrong. Can anyone help?!! > > >Also, using the Unix libray is probably overkill for this kind of thing. > You might want to switch to the Pervasives library, which is >automatically included with all ocaml programs. > >There is a whole group for asking beginning OCaml questions: > >http://groups.yahoo.com/group/ocaml_beginners/ > >