caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Unix.lseek versus Pervasives.pos
@ 2003-03-19 18:36 cashin
  2003-03-19 18:48 ` Nicolas George
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: cashin @ 2003-03-19 18:36 UTC (permalink / raw)
  To: caml-list

Sorry if this shows up as a duplicate.  

Basile STARYNKEVITCH <basile@starynkevitch.net> 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 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
  

... 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) {
        perror("open");
        exit(EXIT_FAILURE);
      }
      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:
  ecashin@uga.edu        |   http://noserose.net/e/pgp/

-------------------
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


^ permalink raw reply	[flat|nested] 10+ messages in thread
[parent not found: <46CF368E-5912-11D7-8289-000A95773ED2@rouaix.org>]
* [Caml-list] Unix.lseek versus Pervasives.pos
@ 2003-03-17 22:45 Shivkumar Chandrasekaran
  2003-03-18  6:54 ` Basile STARYNKEVITCH
  0 siblings, 1 reply; 10+ messages in thread
From: Shivkumar Chandrasekaran @ 2003-03-17 22:45 UTC (permalink / raw)
  To: caml-list

Hi,

Currently I am trying to handle "LargeFiles" while marshalling caml 
values and I have run into this incidental problem (nothing to do with 
LargeFile). If I open a file with "open_out_bin", write to it using 
"output_value" and then try to determine the position in the file using 
"pos", I get the correct value. However, if I use Unix.lseek thus

Unix.lseek (Unix.descr_of_out_channel fd_out) 0 Unix.SEEK_CUR

I get a different value (so far always 0) than the one I get from

pos fd_out

The manual does not seem to help. Any advice will be appreciated. 
Thanks,

--shiv--

-------------------
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


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2003-03-19 20:27 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-19 18:36 [Caml-list] Unix.lseek versus Pervasives.pos cashin
2003-03-19 18:48 ` Nicolas George
2003-03-19 19:01   ` cashin
2003-03-19 18:55 ` Ken Rose
2003-03-19 19:08 ` Basile STARYNKEVITCH
     [not found] <46CF368E-5912-11D7-8289-000A95773ED2@rouaix.org>
2003-03-18 17:35 ` Shivkumar Chandrasekaran
2003-03-18 17:39 ` Shivkumar Chandrasekaran
2003-03-19 20:27   ` Xavier Leroy
  -- strict thread matches above, loose matches on Subject: below --
2003-03-17 22:45 Shivkumar Chandrasekaran
2003-03-18  6:54 ` Basile STARYNKEVITCH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).