Unable to write to file... (example from "pickaxe book", secondedition, p128)

J

John Maclean

Following the example from "pickaxe book", second edition, p128

#!/usr/bin/ruby
File.open("testfile", "r") do |file|
while line = file.gets
puts line
end
end

produces
copy.rb:2:in `initialize': No such file or directory - testfile (Errno::ENOENT)
from copy.rb:2
 
H

HT de Beer

Following the example from "pickaxe book", second edition, p128

#!/usr/bin/ruby
File.open("testfile", "r") do |file|
while line = file.gets
puts line
end
end

produces
copy.rb:2:in `initialize': No such file or directory - testfile
(Errno::ENOENT) from copy.rb:2

The problem is clear. You are trying to read a file which does not exist. So,
to solve the `problem', you create a file named testfile, put some text in
it, and there you go,
 
Z

zdennis

John said:
Following the example from "pickaxe book", second edition, p128

#!/usr/bin/ruby
File.open("testfile", "r") do |file|
while line = file.gets
puts line
end
end

produces
copy.rb:2:in `initialize': No such file or directory - testfile (Errno::ENOENT)
from copy.rb:2

John, you need the file named "testfile" to exist before you can run that code. It can't open a file
if it doesn't exist!

Zach
 
J

John Maclean

Thanks! The light shines. So how can i -write- to a file using the code below as an example?
 
J

Jacob Fugal


Well you need more than that. For one thing the file.gets will fail if
file was opened in write mode. Also the puts is going to STDOUT, not
the file. You probably want something like this:

#!/usr/bin/ruby
File.open("testfile", "w") do |file|
while line =3D gets
file.puts line
end
end

This will get input from STDIN and write it out to test file. Program
terminates on EOF marker (CTRL-D when run interactively).

Jacob Fugal
 
M

Mike Fletcher

John said:
Thanks! The light shines. So how can i -write- to a file using the code
below as an example?

Open a file for writing

File.open( "infile", "r" ) | in |
File.open( "outfile", "w" ) | out |
while line = in.gets
out.print line
end
end
end

See the docs for IO which explain all the mode flags ("r", "w", "a",
etc.).
 
D

Diego Algorta Casamayou

Jacob Fugal escribi=F3:
=20
Well you need more than that. For one thing the file.gets will fail if
file was opened in write mode. Also the puts is going to STDOUT, not
the file. You probably want something like this:
=20
#!/usr/bin/ruby
File.open("testfile", "w") do |file|
while line =3D gets
file.puts line
end
end
=20
This will get input from STDIN and write it out to test file. Program
terminates on EOF marker (CTRL-D when run interactively).

That's right. I focused my reply on on the open method, but missed the=20
full example.

Diego
 
L

Li Chen

Mike said:
Open a file for writing

File.open( "infile", "r" ) | in |
File.open( "outfile", "w" ) | out |
while line = in.gets
out.print line
end
end
end

See the docs for IO which explain all the mode flags ("r", "w", "a",
etc.).

Do you think this script work?

Li
 
M

Marcel Ward

Mike said:
John Maclean wrote:
[snip]

File.open( "infile", "r" ) | in |
File.open( "outfile", "w" ) | out |
while line = in.gets
out.print line
end
end
end

Do you think this script work?

It fails because "in" is a reserved word in Ruby and cannot be used as
a variable name. Try changing the two instances of "in" to "inp" and
it will work.
 
M

Mike Fletcher

Li said:
Do you think this script work?

As is no (I blame lack of caffeine and/or sleep); but with "do" inserted
in the right two places and the reserved word "in" replaced with
something that's not a reserved word ( say "inf" ) it works just fine.

One should take all example code posted in haste to mailing lists with a
grain of salt.
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top