How to determine EOF with sysread?

Hi:

How does one determine the eof when using
sysread. The example below seems to indicate
that using #eof? corrupts the file pointer.

buff_size = File.stat(ARGV[0]).blksize
File.open(ARGV[0]) { |f|

until f.eof? ## this statement corrupts f

buff = f.sysread(buff_size)
print buff

end

}

Thanks

···


Jim Freeze
If only I had something clever to say for my comment…
~

How does one determine the eof when using sysread. The example below
seems to indicate that using #eof? corrupts the file pointer.

buff_size = File.stat(ARGV[0]).blksize
File.open(ARGV[0]) { |f|
# until f.eof? ## this statement corrupts f
    buff = f.sysread(buff_size)
    print buff
# end
}

file = '/etc/passwd'
File.open(file) do |f|
  begin
    buff_size = f.stat.blksize
    while buff = f.sysread(buff_size)
      print buff
    end
  rescue EOFError
    # done reading
  end
end

-sc

···

--
Sean Chittenden

So don’t use eof?. Use the EOFError that sysread raises
at the end of file:

buff_size = File.stat(ARGV[0]).blksize
File.open(ARGV[0]) { |f|
begin
loop { buff = f.sysread(buff_size); print buff }
rescue EOFError
print "\nDone.\n"
end
}

···

On Sunday 07 July 2002 08:58 pm, Jim Freeze wrote:

Hi:

How does one determine the eof when using
sysread. The example below seems to indicate
that using #eof? corrupts the file pointer.


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE

Hi,

“Jim Freeze” jim@freeze.org wrote in message
news:20020707235655.A1477@freeze.org

Hi:

How does one determine the eof when using
sysread. The example below seems to indicate
that using #eof? corrupts the file pointer.

buff_size = File.stat(ARGV[0]).blksize
File.open(ARGV[0]) { |f|

until f.eof? ## this statement corrupts f

buff = f.sysread(buff_size)
print buff

end

}

How about?

File.open(ARGV[0]) { |f|
while true
begin
buff = f.sysread(buff_size)
print buff
rescue EOFError
break
end
end
}

Or

File.open(ARGV[0]) { |f|
begin
while buff = f.sysread(buff_size)
print buff
end
rescue EOFError
end
}

Thanks


Jim Freeze
If only I had something clever to say for my comment…
~

Park Heesob

Hi:

How does one determine the eof when using
sysread. The example below seems to indicate
that using #eof? corrupts the file pointer.

So don’t use eof?. Use the EOFError that sysread raises
at the end of file:

Thanks all for the answers.
Does it strike anyone as bad practice
to use a rescue for control flow?
I thought a more explicit test method would
have been provided.

Jim

···

On Mon, Jul 08, 2002 at 01:35:46PM +0900, Ned Konz wrote:

On Sunday 07 July 2002 08:58 pm, Jim Freeze wrote:

buff_size = File.stat(ARGV[0]).blksize
File.open(ARGV[0]) { |f|
begin
loop { buff = f.sysread(buff_size); print buff }
rescue EOFError
print "\nDone.\n"
end
}


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE


Jim Freeze
If only I had something clever to say for my comment…
~

Thanks all for the answers. Does it strike anyone as bad practice
to use a rescue for control flow? I thought a more explicit test
method would have been provided.

My C roots tell me, yes, but my Ruby roots tell me no. Having an
exception thrown/caught is okay practice in Ruby. -sc

···

--
Sean Chittenden

Hi –

Does it strike anyone as bad practice
to use a rescue for control flow?

Except for speed considerations, I personally prefer this:

begin
object.meth
rescue NoMethodError
# …
end

to this:

if object.respond_to?(meth) then object.meth end

but that’s partly because I really dislike the latter idiom and try to
get around it whenever possible.

David

···

On Mon, 8 Jul 2002, Jim Freeze wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

It still seems wrong to me, since an end of file condition is an
expected situation, not an exceptional situation.

shrug

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.07.08 at 08.39.15

···

On Mon, 8 Jul 2002 14:47:25 +0900, Sean Chittenden wrote:

Thanks all for the answers. Does it strike anyone as bad practice
to use a rescue for control flow? I thought a more explicit test
method would have been provided.

My C roots tell me, yes, but my Ruby roots tell me no. Having an
exception thrown/caught is okay practice in Ruby.

Austin Ziegler wrote:

Thanks all for the answers. Does it strike anyone as bad practice
to use a rescue for control flow? I thought a more explicit test
method would have been provided.

My C roots tell me, yes, but my Ruby roots tell me no. Having an
exception thrown/caught is okay practice in Ruby.

It still seems wrong to me, since an end of file condition is an
expected situation, not an exceptional situation.

shrug

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.07.08 at 08.39.15

But eof is the exception when reading a file, because a read will
usually succeed.
This sort of thing is used a lot on VAXes (if they still exist) and
early mini / mainframe designs.

I expect programs to have bugs, but I can’t predict what and where or I
would have written the program differently. Therefore they are
exceptions even though they are expected.

···

On Mon, 8 Jul 2002 14:47:25 +0900, Sean Chittenden wrote:

I look at exceptions as communicating “out of band” information. In
the case of sysread(), the “in band” information is the data you’re
reading. Everything else (EOF, failures, etc.) is out of band.

If you don’t use an exception you have to test all over the place.
This can also require status codes to be passed up several levels.

The main impetus behind using exceptions is this:

  • we can detect exceptional conditions easiest at the lowest levels
    (in this case, inside sysread).
  • but the callers of these low-level routines generally don’t know
    what the policy for dealing with the exceptional conditions is
  • so we want some caller, possibly quite a bit up the caller chain, to
    be notified
  • but this would require passing status all the way back up, or
    (worse) testing status at higher levels

So it’s no accident that Ruby, Smalltalk, C++, and Java (I don’t know
about the specifics of exception handling in CLU, Ada, or PL/I) ended
up with essentially the same exception model (except, of course, that
most Smalltalks allow resumption as well as termination).

Another point is that code using exceptions is generally smaller and
faster than code using status code propagation.

···

On Monday 08 July 2002 05:40 am, Austin Ziegler wrote:

On Mon, 8 Jul 2002 14:47:25 +0900, Sean Chittenden wrote:

Thanks all for the answers. Does it strike anyone as bad
practice to use a rescue for control flow? I thought a more
explicit test method would have been provided.

My C roots tell me, yes, but my Ruby roots tell me no. Having an
exception thrown/caught is okay practice in Ruby.

It still seems wrong to me, since an end of file condition is an
expected situation, not an exceptional situation.


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE

(snip)

So it’s no accident that Ruby, Smalltalk, C++, and Java (I don’t know
about the specifics of exception handling in CLU, Ada, or PL/I) ended
up with essentially the same exception model (except, of course, that
most Smalltalks allow resumption as well as termination).
(snip)

I haven’t messed with PL/I in over 18 years, but my memory is that EOF
is handled by an “on unit,” a special statement label that is branched
to when certain conditions occur. The syntax is something like:

ON EOF MYFILE DO;
/* whatever */
END;

Looks like an exception to me.

···

On Mon, 08 Jul 2002 14:58:13 GMT, Ned Konz ned@bike-nomad.com wrote:

You have a point, but everyone that provided code to answer my question
assumed there was no errors in reading. In other words, if we use
the exception to catch an EOF, we have to be able to distinquish it
from a ‘real’ error. So, if the file is 100 bytes, and it only reads
fifty, I would like to know about it.

Seems I would have to add some code to sum the bytes read and re-throw
if the exception is caught before the end of the file is reached.

···

On Mon, Jul 08, 2002 at 10:05:34PM +0900, Peter Hickman wrote:

Austin Ziegler wrote:

On Mon, 8 Jul 2002 14:47:25 +0900, Sean Chittenden wrote:

Thanks all for the answers. Does it strike anyone as bad practice
to use a rescue for control flow? I thought a more explicit test
method would have been provided.

My C roots tell me, yes, but my Ruby roots tell me no. Having an
exception thrown/caught is okay practice in Ruby.

It still seems wrong to me, since an end of file condition is an
expected situation, not an exceptional situation.

– Austin Ziegler, austin@halostatue.ca on 2002.07.08 at 08.39.15

But eof is the exception when reading a file, because a read will
usually succeed.
This sort of thing is used a lot on VAXes (if they still exist) and
early mini / mainframe designs.

I expect programs to have bugs, but I can’t predict what and where or I
would have written the program differently. Therefore they are
exceptions even though they are expected.


Jim Freeze
If only I had something clever to say for my comment…
~

Depends on whether you know how big it needs to be.
But if you just want to catch read errors, just catch SystemError
somewhere (possibly higher). No need to re-throw at this level.

···

On Monday 08 July 2002 01:34 pm, Jim Freeze wrote:

You have a point, but everyone that provided code to answer my
question assumed there was no errors in reading. In other words, if
we use the exception to catch an EOF, we have to be able to
distinquish it from a ‘real’ error. So, if the file is 100 bytes,
and it only reads fifty, I would like to know about it.

Seems I would have to add some code to sum the bytes read and
re-throw if the exception is caught before the end of the file is
reached.


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE