Newbie question on file reading

  • Thread starter Rajarshi Chakravarty
  • Start date
R

Rajarshi Chakravarty

Hi,
I have got the code to read a file line by line amd it works too but I
don't understand it.

fp = File.open(dataFile, "r")
fp.each_line do |line|
puts line
end

My question is: the "open" method is not mentioned in the doc of File
class
http://ruby-doc.org/core/classes/File.html
What type does it return?

My 2nd question is how can I read a file char by char. Basically the
file doesn't have line breaks.

Please help..
 
R

Robert Klemme

2010/8/24 Peter Hickman said:
I believe it comes from the IO class instead
http://ruby-doc.org/core/classes/IO.html

Yes, this is true.

Rajarshi, you can make your life a bit simpler by doing

File.foreach(dataFile) do |line|
puts line
end

If you actually need to open the file and manipulate the File / IO
object directly (e.g. because you want to rewind), you can use the
block form of File.open (or Io_Open):

File.open(dataFile, "r") do |fp|
fp.each_line do |line|
puts line
end
end

This ensures the file is properly closed which was not the case in
your original example. You can find more at

http://blog.rubybestpractices.com/posts/rklemme/001-Using_blocks_for_Robustness.html

And if you want to know the internal workings you can look here

http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html

Kind regards

robert
 
M

Martin DeMello

My 2nd question is how can I read a file char by char. Basically the
file doesn't have line breaks.

How big is your file? IO.read("/path/to/file") will read the entire
file into a string. If you really want to read it character by
character

File.open("/path/to/file") do |f|
while true do
begin
a = f.readchar
# do something with a
rescue EOFError
break
end
end
end

martin
 
R

Rajarshi Chakravarty

Thank you, Robert.
The links were most helpful.

I need to read char substrings from a file that has no line breaks in
it.
Is the code below the best way to do it?

File.foreach(dataFile) do |line|
puts line
end

Will the "line" var give the entire text in the file?

Another question (this is posted on the link you gave)
Instead of doing File.foreach(dataFile) can I not do
IO.foreach(dataFile) do |line|?
 
R

Robert Klemme

2010/8/24 Rajarshi Chakravarty said:
Thank you, Robert.

Your welcome!
The links were most helpful.
Good!

I need to read char substrings from a file that has no line breaks in
it.

Note that you can use a different separator character:

File.foreach data_file, "SEPARATOR" do |entry|
puts entry
end

The same works for File.each:

File.open... do |io|
io.each "SEPARATOR" do |entry|
...
end
end
Is the code below the best way to do it?

File.foreach(dataFile) do |line|
=A0puts line
end

Will the "line" var give the entire text in the file?

No, because this will try to load lines separated by \n. If you want
the whole text of the file in one go you can do

contents =3D File.read data_file

However, if the file is large, a blocked approach usually yields better res=
ults:

File.open data_file do |io|
buffer =3D ""

while io.read(1024, buffer)
buffer.each_char {|ch| p ch} # note, ch is really a String of length 1
end
end
Another question (this is posted on the link you gave)
Instead of doing File.foreach(dataFile) can I not do
IO.foreach(dataFile) do |line|?

Yes, File and IO are interchangeable here. Personally I prefer to use
File when dealing with files just for documentation reasons.

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Rajarshi Chakravarty

In continuation with my questions about reading files from the file
system...

It seems Dir[File.join(root, subDir, "*")] gives the full path name of
all files in the sub directory

Can I modify the expression to give just the basename?
 
R

Robert Klemme

2010/8/26 Rajarshi Chakravarty said:
In continuation with my questions about reading files from the file
system...

It seems Dir[File.join(root, subDir, "*")] gives the full path name of
all files in the sub directory

Can I modify the expression to give just the basename?

You can feed the result through #map and File.basename.

Cheers

robert
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top