Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] Unix.lseek versus Pervasives.pos
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Ken Rose <kenrose@t...>
Subject: Re: [Caml-list] Unix.lseek versus Pervasives.pos
cashin@cs.uga.edu wrote:
> 
> Flushes are for writes, but even when using a test program that just
> reads, zero is returned when it appears that it shouldn't return zero.
> Compare the short ocaml program below to the comparable C version.
> 
> The ocaml version has lseek returning position zero after reading 10
> bytes from the file.
> 
>   ecashin@meili seek-tell$ ./test
>   after reading 10 chars: "let main =", position is 0
>   ecashin@meili seek-tell$ cat main.ml
>   let main =
>     let fd = Unix.openfile "main.ml" [Unix.O_RDONLY] 0
>     and buf = String.create 1024 in
>     Printf.printf "after reading %d chars: \"%s\", position is %d\n"
>       (UnixLabels.read fd ~buf ~pos:0 ~len:10)
>       buf
>       (UnixLabels.lseek fd 0 ~mode:Unix.SEEK_CUR)
>   ;;
> 
>   main
> 

It looks like you're getting bitten by the order of evaluation of
function arguments.  

$ cat main.ml
let main = 
    let fd = Unix.openfile "main.ml" [Unix.O_RDONLY] 0 
    and buf = String.create 1024 in
    let r = (UnixLabels.read fd ~buf ~pos:0 ~len:10) in
    Printf.printf "after reading %d chars: \"%s\", position is %d\n"
      r
      buf
      (UnixLabels.lseek fd 0 ~mode:Unix.SEEK_CUR)
  ;;

main

$ ./a.out
after reading 10 chars: "let main =", position is 10
$

 - ken

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners