[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Unable to write to file... (example from "pickaxe book", second edition, p128

John Maclean

1/19/2006 10:50:00 AM

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 Maclean
MSc (DIC)
07739 171 531



11 Answers

HT de Beer

1/19/2006 10:55:00 AM

0

On Thursday 19 January 2006 11:49, John Maclean wrote:
> 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,

--
HT de Beer


Zach Dennis

1/19/2006 10:57:00 AM

0

John Maclean wrote:
> 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


John Maclean

1/19/2006 11:21:00 AM

0

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

On Thu, 19 Jan 2006 19:56:55 +0900
zdennis <zdennis@mktec.com> wrote:

> John Maclean wrote:
> > 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
>
>
>


--
John Maclean
MSc (DIC)
07739 171 531



Marcin Mielzynski

1/19/2006 11:36:00 AM

0

John Maclean wrote:

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

File.open("testfile", "w")

:D

lopex

Diego Algorta Casamayou

1/19/2006 11:36:00 AM

0

John Maclean escribió:
> Thanks! The light shines. So how can i -write- to a file using the code below as an example?
>
Change
>>> File.open("testfile", "r") do |file|
to
>>> File.open("testfile", "w") do |file|

Read:
http://www.ruby-doc.org/core/classes...
http://www.ruby-doc.org/core/class...

Diego

> On Thu, 19 Jan 2006 19:56:55 +0900
> zdennis <zdennis@mktec.com> wrote:
>
>> John Maclean wrote:
>>> 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
>>
>>
>>
>
>



Jacob Fugal

1/19/2006 4:10:00 PM

0

On 1/19/06, Diego Algorta Casamayou <ruby@dac.e4ward.com> wrote:
> Change
> >>> File.open("testfile", "r") do |file|
> to
> >>> File.open("testfile", "w") do |file|

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 = 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


Mike Fletcher

1/19/2006 4:29:00 PM

0

John Maclean wrote:
> 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.).

--
Posted via http://www.ruby-....


Diego Algorta Casamayou

1/19/2006 4:38:00 PM

0

Jacob Fugal escribió:
> On 1/19/06, Diego Algorta Casamayou <ruby@dac.e4ward.com> wrote:
>> Change
>> >>> File.open("testfile", "r") do |file|
>> to
>> >>> File.open("testfile", "w") do |file|
>
> 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 = 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).

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

Diego



Li Chen

11/3/2006 5:24:00 PM

0

Mike Fletcher wrote:
> John Maclean wrote:
>> 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.).

Do you think this script work?

Li

--
Posted via http://www.ruby-....

Marcel Ward

11/3/2006 6:08:00 PM

0

On 03/11/06, Li Chen <chen_li3@yahoo.com> wrote:
> Mike Fletcher wrote:
> > 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.

--
Marcel