Sysread in perl

Discussion in 'Perl Misc' started by Tony, May 11, 2004.

  1. Tony

    Tony Guest

    Hi,

    I am trying to read the last 500 bytes of a file. I'm trying to use
    sysread command, I have read the docs for it on cpan and it seems the
    right thing to use.
    My only problem is when i spcify a negative offset it fails with the
    following error "Offset outside string at test.pl line 30.".

    here is the line it is complaing about

    sysread(F,$string,500,-500);

    I have tried escaping the minus sign , putting it into a var and using
    eval and they all come back with the same error.

    Heres the paragraph from cpans docs

    "An OFFSET may be specified to place the read data at some place in
    the string other than the beginning. A negative OFFSET specifies
    placement at that many characters counting backwards from the end of
    the string. A positive OFFSET greater than the length of SCALAR
    results in the string being padded to the required size with "\0"
    bytes before the result of the read is appended."

    should this work or is it just me reading the distructions wrong.


    thanks


    Tony
     
    Tony, May 11, 2004
    #1
    1. Advertising

  2. Tony

    gnari Guest

    "Tony" <> wrote in message
    news:...
    > Hi,
    >
    > I am trying to read the last 500 bytes of a file. I'm trying to use
    > sysread command, I have read the docs for it on cpan and it seems the
    > right thing to use.
    > My only problem is when i spcify a negative offset it fails with the
    > following error "Offset outside string at test.pl line 30.".


    this error message should have been a hint

    >
    > Heres the paragraph from cpans docs
    >
    > "An OFFSET may be specified to place the read data at some place in
    > the string other than the beginning. A negative OFFSET specifies
    > placement at that many characters counting backwards from the end of
    > the string. A positive OFFSET greater than the length of SCALAR
    > results in the string being padded to the required size with "\0"
    > bytes before the result of the read is appended."


    notice how it says 'string' but not 'file' everywhere

    >
    > should this work or is it just me reading the distructions wrong.


    a) if you expect sysread with OFFSET -500 to read last 500
    bytes of file, then you are reading the 'distructions' wrong,
    and should lookup sysseek

    b) if you expect it to read into the last 500 bytes of your
    string, then you need to make sure the string is at lead 500
    bytes to start with, as the \0 padding only applies to
    positive OFFSETS

    gnari
     
    gnari, May 11, 2004
    #2
    1. Advertising

  3. Tony

    Thomas Kratz Guest

    Tony wrote:
    > Hi,
    >
    > I am trying to read the last 500 bytes of a file. I'm trying to use
    > sysread command, I have read the docs for it on cpan and it seems the
    > right thing to use.
    > My only problem is when i spcify a negative offset it fails with the
    > following error "Offset outside string at test.pl line 30.".
    >
    > here is the line it is complaing about
    >
    > sysread(F,$string,500,-500);
    >
    > I have tried escaping the minus sign , putting it into a var and using
    > eval and they all come back with the same error.
    >
    > Heres the paragraph from cpans docs
    >
    > "An OFFSET may be specified to place the read data at some place in
    > the string other than the beginning. A negative OFFSET specifies
    > placement at that many characters counting backwards from the end of
    > the string. A positive OFFSET greater than the length of SCALAR
    > results in the string being padded to the required size with "\0"
    > bytes before the result of the read is appended."
    >
    > should this work or is it just me reading the distructions wrong.


    It's you ;-) You are *reading* from the filehandle ( called F in your
    example) and *writing* to $string. The offset specifies where in $string
    you want to put the number of bytes specified in the 3rd parameter, not
    where you start reading from the file.

    If you want to read from the end of a file look at

    perldoc -f seek
    perldoc -q tail

    or the modules

    File::Tail
    File::ReadBackwards

    on CPAN

    Thomas

    --
    open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
    STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
    print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</ #.>s^~h<t< ..~. ...c.^..
    &&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#....>>e>r^..>l^...>k^..
     
    Thomas Kratz, May 11, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Fulton

    sysread and buffered I/O

    Hal Fulton, Jul 21, 2004, in forum: Ruby
    Replies:
    39
    Views:
    667
    Tanaka Akira
    Jul 24, 2004
  2. William E. Rubin
    Replies:
    3
    Views:
    128
    William E. Rubin
    Dec 8, 2005
  3. J. Romano

    Perl's read() vs. sysread()

    J. Romano, Feb 6, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    2,171
    Walter Roberson
    Feb 6, 2004
  4. Bean
    Replies:
    2
    Views:
    510
    Anno Siegel
    Nov 24, 2004
  5. john
    Replies:
    7
    Views:
    249
    Brian McCauley
    Mar 4, 2005
Loading...

Share This Page