Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Unable to write to file... (example from "pickaxe book", secondedition, p128)

Reply
Thread Tools

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

 
 
John Maclean
Guest
Posts: n/a
 
      01-19-2006
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



 
Reply With Quote
 
 
 
 
HT de Beer
Guest
Posts: n/a
 
      01-19-2006
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


 
Reply With Quote
 
 
 
 
zdennis
Guest
Posts: n/a
 
      01-19-2006
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


 
Reply With Quote
 
John Maclean
Guest
Posts: n/a
 
      01-19-2006
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 <(E-Mail Removed)> 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



 
Reply With Quote
 
Marcin Mielżyński
Guest
Posts: n/a
 
      01-19-2006
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")



lopex
 
Reply With Quote
 
Diego Algorta Casamayou
Guest
Posts: n/a
 
      01-19-2006
John Maclean escribi=F3:
> Thanks! The light shines. So how can i -write- to a file using the code=

below as an example?
>=20

Change
>>> File.open("testfile", "r") do |file|

to
>>> File.open("testfile", "w") do |file|


Read:
http://www.ruby-doc.org/core/classes/File.html
http://www.ruby-doc.org/core/classes/IO.html

Diego

> On Thu, 19 Jan 2006 19:56:55 +0900
> zdennis <(E-Mail Removed)> wrote:
>=20
>> John Maclean wrote:
>>> Following the example from "pickaxe book", second edition, p128
>>>
>>> #!/usr/bin/ruby=20
>>> File.open("testfile", "r") do |file|
>>> while line =3D file.gets
>>> puts line
>>> end
>>> end
>>>
>>> produces
>>> copy.rb:2:in `initialize': No such file or directory - testfile (Errn=

o::ENOENT)
>>> from copy.rb:2
>>>

>> John, you need the file named "testfile" to exist before you can run t=

hat code. It can't open a file=20
>> if it doesn't exist!
>>
>> Zach
>>
>>
>>

>=20
>=20




 
Reply With Quote
 
Jacob Fugal
Guest
Posts: n/a
 
      01-19-2006
On 1/19/06, Diego Algorta Casamayou <(E-Mail Removed)4ward.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 =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


 
Reply With Quote
 
Mike Fletcher
Guest
Posts: n/a
 
      01-19-2006
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-forum.com/.


 
Reply With Quote
 
Diego Algorta Casamayou
Guest
Posts: n/a
 
      01-19-2006
Jacob Fugal escribi=F3:
> On 1/19/06, Diego Algorta Casamayou <(E-Mail Removed)4ward.com> wrote:
>> Change
>> >>> File.open("testfile", "r") do |file|

>> to
>> >>> File.open("testfile", "w") do |file|

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



 
Reply With Quote
 
Li Chen
Guest
Posts: n/a
 
      11-03-2006
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-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to access HDD / Windows unable to complete format rmundy Hardware 1 08-13-2010 08:57 AM
unable to capture all the HTML of my page,or I'm unable to save it to disk Jake Barnes Javascript 6 02-08-2006 02:38 PM
Unable to renew IP address/unable to connect to wireless network =?Utf-8?B?SmFzZXlCb3k=?= Wireless Networking 1 12-22-2005 04:28 AM
Unable to see machines on the ethernet; Unable to find printers =?Utf-8?B?Sm9obiBN?= Wireless Networking 1 06-07-2005 03:32 AM
Error BC31019: Unable to write to output file '<filename>' Joel Finkel ASP .Net 1 08-15-2003 03:48 PM



Advertisments