English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-01-25 (22:41)
From: Karl Zilles <zilles@1...>
Subject: Re: [Caml-list] open_file
(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: