Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sysread in perl

Reply
Thread Tools

Sysread in perl

 
 
Tony
Guest
Posts: n/a
 
      05-11-2004
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
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      05-11-2004
"Tony" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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




 
Reply With Quote
 
 
 
 
Thomas Kratz
Guest
Posts: n/a
 
      05-11-2004
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^..
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invalid arg to sysread deep within protocol.rb William E. Rubin Ruby 3 12-08-2005 09:53 PM
perl problem with select and non-blocking sysread from multiple pipes john Perl Misc 7 03-04-2005 02:34 PM
sysread(socket..) problem on perl 5.8.0 linux Bean Perl Misc 2 11-24-2004 11:19 AM
sysread and buffered I/O Hal Fulton Ruby 39 07-24-2004 03:45 AM
Perl's read() vs. sysread() J. Romano Perl Misc 3 02-06-2004 07:32 PM



Advertisments