[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] read() and sysread()



ajf@afco.demon.co.uk (Alan Fry) writes:
>Doing this with read():
>
>        open(IN, "somefile.txt");
>        $offset = 1024; #for instance
>        read(IN, $doc, 100, $offset);
>        print $doc;
>
>prints lines of binary characters (?) followed by the _first_ 100
>characters of the file. I expected 100 characters from '$offset' into the
>file. Am I expecting the wrong thing or is MacPerl misbehaving here?

You're expecting wrong. $offset is into the scalar, not into the file:

     read(FILEHANDLE,SCALAR,LENGTH,OFFSET)
             Attempts to read LENGTH bytes of data into  variable
             SCALAR  from  the specified FILEHANDLE.  Returns the
             number of bytes actually read, or undef if there was
             an  error.   SCALAR  will  be grown or shrunk to the
             length actually read.  An OFFSET may be specified to
             place  the  read  data  at some other place than the
             beginning of the  string. 

To get an offset into the file, use seek().

Matthias

-----
Matthias Neeracher   <neeri@iis.ee.ethz.ch>   http://www.iis.ee.ethz.ch/~neeri
  "Tossing around Slavic swearwords?  Tsk tsk.  Nekulturny peasants."
                                      -- Paul J. Zanca <pjz@sccsi.com>