Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: The Unix module & records
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Markus Mottl <mottl@m...>
Subject: Re: The Unix module & records
> 1: let main () =
> 2:        let file_info : Unix.stats = Unix.stat Sys.argv.(1) in
> 3:        let fd = Unix.openfile Sys.argv.(1) [ Unix.O_RDONLY ] perm in
> 4:        let buffer = ref "" in
> 5:        let bytes_read = Unix.read (fd) (buffer) (file_info.st_size) in
> 	<snipped>
> 
> But it fails with the error "Unbound record field label st_size" on line 5.
> I can't explain why this is happening; looking at the Unix.ml file, I see
> the st_size field in the Unix.stats record.  Any suggestions would be
> appreciated.

Unless you open the Unix-library with "open Unix", you need to write the
fully qualified name:

  file_info.Unix.st_size

instead of

  file_info.st_size

Furthermore, strings are not resizable: so you cannot read in anything into
an empty one. Btw., you need not write parenthesis around function
arguments if they are just one word (= an evaluated expression).

Try something like the following code:

  let read_file name =
    let size = (Unix.stat name).Unix.st_size in
    let buf = String.create size
    and file = open_in name in
    really_input file buf 0 size;
    close_in file;
    buf

Best regards,
Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl