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
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: 2003-03-19 (18:36)
From: cashin@c...
Subject: Re: [Caml-list] Unix.lseek versus Pervasives.pos
Sorry if this shows up as a duplicate.  

Basile STARYNKEVITCH <> writes:

> You apparently forgot to flush the channel.

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
  let main = 
    let fd = Unix.openfile "" [Unix.O_RDONLY] 0 
    and buf = String.create 1024 in
    Printf.printf "after reading %d chars: \"%s\", position is %d\n"
      ( fd ~buf ~pos:0 ~len:10)
      (UnixLabels.lseek fd 0 ~mode:Unix.SEEK_CUR)

... but in the C version you get the expected position reported.
  ecashin@meili seek-tell$ ./test 
  after reading "#include <" lseek returns 10
  ecashin@meili seek-tell$ cat main.c
  #include <stdio.h>
  #include <stdlib.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <fcntl.h>
  #include <unistd.h>
  int main(void)
      int fd = open("main.c", O_RDONLY);
      char buf[1024];
      if (fd == -1) {
      memset(buf, '\0', sizeof(buf));
      read(fd, buf, 10);
      printf("after reading \"%s\" lseek returns %d\n",
             buf, (int) lseek(fd, 0, SEEK_CUR));
      return 0;

--Ed L Cashin            |   PGP public key:        |

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: