Read only a few bytes from file

T

toulax

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
 
A

Alex Young

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"
 
R

Rob Biedenharn

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.com/rails/

<!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 • May 17, 2007 - May 20, 2007 •
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>



$ irbRailsConf 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://agileconsultingllc.com
(e-mail address removed)
 
T

toulax

Initial contents ofhttp://conferences.oreillynet.com/rails/

<!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 • May 17, 2007 - May 20, 2007 •
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
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://agileconsultingllc.com
(e-mail address removed)

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
 
T

Timothy Hunter

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.
 
T

toulax

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?
 
G

Gary Wright

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.com") { |f| f.read(8) }

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



Gary Wright
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,183
Latest member
OrderGlycoEase

Latest Threads

Top