Hello everybody,
I would like to load the exact content of a file in a buffer (with the
same carriage return). To do so, I had trying this:
file = File.open(’/home/my_file’)
To verify if that work normally, I had try to add this for display the
content of my_file:
puts file
But my_file was not been printed, and I got: #<File:0x28d70>
If you know a way to do so, please help me
Thanks.
Zang’
Alle Monday 03 March 2008, Zangief I. ha scritto:
puts file
But my_file was not been printed, and I got: #<File:0x28d70>
If you know a way to do so, please help me
Thanks.
Zang’
To get the contents of a file, opening it is not enough. You need to
explicitly read its contents. There are many methods which allow to
access the
contents of a file: read, readlines, each_line, each_byte, File.read,
File.readlines, File.foreach (the last three are class methods). They’re
documented under class IO (from which File is derived). If you only need
to
read the contents of the file you don’t need to use File.open at all,
but
simply use File.read:
contents = File.read(’/home/my_file’/)
I hope this helps
Stefano
On Mon, Mar 3, 2008 at 12:20 PM, Zangief I. [email protected] wrote:
puts file
But my_file was not been printed, and I got: #<File:0x28d70>
try:
File.open(“/home/my_file”, “r”) do |infile|
while (line = infile.gets)
puts “#{counter}: #{line}”
counter = counter + 1
end
end
On Mar 3, 2008, at 12:20 PM, Zangief I. wrote:
puts file
But my_file was not been printed, and I got: #<File:0x28d70>
If you know a way to do so, please help me
Try
buffer = IO.read('/home/my_file')
or
buffer = File.read('/home/my_file')
They both do the same thing because File inherits ‘read’ from IO.
Regards, Morton
Thanks! ‘read’ method works very well