[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Read only a few bytes from file

toulax@gmail.com

3/25/2007 11:06:00 AM

Is there any way to read only a determined amount of bytes from a
specific file? I actually want to do that to a URL, but I assume the
procedure would be the same.

If someone could at least point me to the right direction that would
be great!

Thanks

6 Answers

Alex Young

3/25/2007 11:18:00 AM

0

toulax@gmail.com wrote:
> Is there any way to read only a determined amount of bytes from a
> specific file? I actually want to do that to a URL, but I assume the
> procedure would be the same.
>
> If someone could at least point me to the right direction that would
> be great!

If you want to read from the start of the file, File#read takes a number
of bytes as a parameter:

irb(main):015:0> File.open('/usr/share/dict/words'){|f| f.read(50)}
=> "\nA\nA's\nAOL\nAOL's\nAachen\nAachen's\nAaliyah\nAaliyah's"

--
Alex

Rob Biedenharn

3/25/2007 1:00:00 PM

0


On Mar 25, 2007, at 7:10 AM, toulax@gmail.com wrote:

> Is there any way to read only a determined amount of bytes from a
> specific file? I actually want to do that to a URL, but I assume the
> procedure would be the same.
>
> If someone could at least point me to the right direction that would
> be great!
>
> Thanks

Initial contents of http://conferences.oreillynet....

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd...
<html xmlns="http://www.w3.org/1999/xhtml...
<head>

<title>RailsConf 2007 &#8226; May 17, 2007 - May 20, 2007 &#8226;
Portland, Oregon</title>

<meta name="description" content="RailsConf 2007 - May 17, 2007
- May 20, 2007 - Portland, Oregon" />
<link href="/styles/railsconf2007.css" rel="stylesheet" type="text/
css" />

<script src="/scripts/functions.js" type="text/javascript"></script>

</head>



$ irb
>> require 'open-uri'
=> true
>> open('http://conferences.oreillynet....') { |f|
?> f.pos = 182
>> puts f.read(14)
>> }
RailsConf 2007
=> nil

open-uri extends Kernel#open to understand how to read from HTTP and
FTP sources
IO#pos= sets the offset
IO#read returns up to the number of bytes requested

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com




toulax@gmail.com

3/25/2007 8:57:00 PM

0

On Mar 25, 9:59 am, Rob Biedenharn <R...@AgileConsultingLLC.com>
wrote:
> On Mar 25, 2007, at 7:10 AM, tou...@gmail.com wrote:
>
> > Is there any way to read only a determined amount of bytes from a
> > specific file? I actually want to do that to a URL, but I assume the
> > procedure would be the same.
>
> > If someone could at least point me to the right direction that would
> > be great!
>
> > Thanks
>
> Initial contents ofhttp://conferences.oreillynet....
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd...
> <html xmlns="http://www.w3.org/1999/xhtml...
> <head>
>
> <title>RailsConf 2007 &#8226; May 17, 2007 - May 20, 2007 &#8226;
> Portland, Oregon</title>
>
> <meta name="description" content="RailsConf 2007 - May 17, 2007
> - May 20, 2007 - Portland, Oregon" />
> <link href="/styles/railsconf2007.css" rel="stylesheet" type="text/
> css" />
>
> <script src="/scripts/functions.js" type="text/javascript"></script>
>
> </head>
>
> $ irb
> >> require 'open-uri'
> => true
> >> open('http://conferences.oreillynet....') { |f|
> ?> f.pos = 182
> >> puts f.read(14)
> >> }
> RailsConf 2007
> => nil
>
> open-uri extends Kernel#open to understand how to read from HTTP and
> FTP sources
> IO#pos= sets the offset
> IO#read returns up to the number of bytes requested
>
> -Rob
>
> Rob Biedenharn http://agileconsult...
> R...@AgileConsultingLLC.com

This works, however it still reads the entire page but only displays
the certain amount of bytes. Is it possible to only load the amount of
bytes that I specify?

Say for instance that I have a 10mb file, how do I get the first few
bytes of this file without having to read it entirely first?

Thanks

Tim Hunter

3/25/2007 9:49:00 PM

0

toulax@gmail.com wrote:
> This works, however it still reads the entire page but only displays
> the certain amount of bytes. Is it possible to only load the amount of
> bytes that I specify?
>
> Say for instance that I have a 10mb file, how do I get the first few
> bytes of this file without having to read it entirely first?
>
Specify the number of bytes you want to the read method. Because of
buffering read may actually read more bytes than you want, but it won't
be all 10mb.

toulax@gmail.com

3/25/2007 10:14:00 PM

0

On Mar 25, 6:49 pm, Timothy Hunter <TimHun...@nc.rr.com> wrote:
> tou...@gmail.com wrote:
> > This works, however it still reads the entire page but only displays
> > the certain amount of bytes. Is it possible to only load the amount of
> > bytes that I specify?
>
> > Say for instance that I have a 10mb file, how do I get the first few
> > bytes of this file without having to read it entirely first?
>
> Specify the number of bytes you want to the read method. Because of
> buffering read may actually read more bytes than you want, but it won't
> be all 10mb.

I tested on a URL with around 200kb and it took the exact same time
reading it all vs reading only 8 bytes. I believe this is done in PHP
via sockets, isn't the same possible with Ruby?

Gary Wright

3/26/2007 3:50:00 AM

0


On Mar 25, 2007, at 6:15 PM, toulax@gmail.com wrote:
> I tested on a URL with around 200kb and it took the exact same time
> reading it all vs reading only 8 bytes. I believe this is done in PHP
> via sockets, isn't the same possible with Ruby?

Absolutely.


require 'open-uri'

text = open("http://www.google...) { |f| f.read(8) }

puts "first eight bytes is: #{text}"



Gary Wright