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

Discussion in 'Ruby' started by John Maclean, Jan 19, 2006.

  1. John Maclean

    John Maclean Guest

    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
    John Maclean, Jan 19, 2006
    #1
    1. Advertising

  2. John Maclean

    HT de Beer Guest

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

    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
    HT de Beer, Jan 19, 2006
    #2
    1. Advertising

  3. John Maclean

    zdennis Guest

    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
    zdennis, Jan 19, 2006
    #3
  4. John Maclean

    John Maclean Guest

    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 <> 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
    John Maclean, Jan 19, 2006
    #4
  5. 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
    Marcin Mielżyński, Jan 19, 2006
    #5
  6. 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 <> 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
    Diego Algorta Casamayou, Jan 19, 2006
    #6
  7. John Maclean

    Jacob Fugal Guest

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

    On 1/19/06, Diego Algorta Casamayou <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
    Jacob Fugal, Jan 19, 2006
    #7
  8. Re: Unable to write to file... (example from "pickaxe book",

    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/.
    Mike Fletcher, Jan 19, 2006
    #8
  9. Jacob Fugal escribi=F3:
    > On 1/19/06, Diego Algorta Casamayou <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
    Diego Algorta Casamayou, Jan 19, 2006
    #9
  10. John Maclean

    Li Chen Guest

    Re: Unable to write to file... (example from "pickaxe book",

    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/.
    Li Chen, Nov 3, 2006
    #10
  11. John Maclean

    Marcel Ward Guest

    Re: Unable to write to file... (example from "pickaxe book",

    On 03/11/06, Li Chen <> 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
    Marcel Ward, Nov 3, 2006
    #11
  12. Re: Unable to write to file... (example from "pickaxe book",

    Li Chen wrote:
    > 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?


    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.

    --
    Posted via http://www.ruby-forum.com/.
    Mike Fletcher, Nov 3, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Maclean
    Replies:
    2
    Views:
    82
    Ross Bamford
    Jan 15, 2006
  2. John Maclean
    Replies:
    35
    Views:
    304
    Donkey Agony
    Jan 24, 2006
  3. Sam'l B
    Replies:
    10
    Views:
    202
  4. Alex Strasheim
    Replies:
    4
    Views:
    114
    Alex Strasheim
    Feb 7, 2007
  5. Yugui (Yuki Sonoda)

    Ruby 1.9.1-p128 is out

    Yugui (Yuki Sonoda), May 12, 2009, in forum: Ruby
    Replies:
    4
    Views:
    137
    Justin Collins
    May 12, 2009
Loading...

Share This Page