Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Karl Zilles <zilles@1...>
Subject: Re: [Caml-list] open_file
(forgot to copy the list, originally) 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 "" [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: