Error when reading file from command prompt

Discussion in 'Ruby' started by Mmcolli00 Mom, Apr 22, 2009.

  1. Hi. I have a program that is supposed to get a filename at the command
    prompt. It reads in the input ok but doesn't initialize to the file. I
    have more code that takes the file and parses it but I did not paste
    that below.

    Question: Is there a step that I am missing in f = File.new(myinput)?
    Thanks MC

    inputFile = print("Enter Filename: ")
    $stdout.flush
    input = gets
    myinput = input.to_s

    f = File.new(myinput) <-- in 'initialize': Invalid argument - filename
    (Errno::EINAL) from crb.rb in 'new'
    --
    Posted via http://www.ruby-forum.com/.
    Mmcolli00 Mom, Apr 22, 2009
    #1
    1. Advertising

  2. Mmcolli00 Mom

    Suresh Kk Guest

    Mmcolli00 Mom wrote:
    > Hi. I have a program that is supposed to get a filename at the command
    > prompt. It reads in the input ok but doesn't initialize to the file. I
    > have more code that takes the file and parses it but I did not paste
    > that below.
    >
    > Question: Is there a step that I am missing in f = File.new(myinput)?
    > Thanks MC
    >
    > inputFile = print("Enter Filename: ")
    > $stdout.flush
    > input = gets
    > myinput = input.to_s
    >
    > f = File.new(myinput) <-- in 'initialize': Invalid argument - filename
    > (Errno::EINAL) from crb.rb in 'new'


    input = gets

    input = gets.chomp
    --
    Posted via http://www.ruby-forum.com/.
    Suresh Kk, Apr 22, 2009
    #2
    1. Advertising

  3. On Wed, Apr 22, 2009 at 6:02 PM, Mmcolli00 Mom <> wrote:
    > Hi. I have a program that is supposed to get a filename at the command
    > prompt. It reads in the input ok but doesn't initialize to the file. I
    > have more code that takes the file and parses it but I did not paste
    > that below.
    >
    > Question: Is there a step that I am missing in f = File.new(myinput)?
    > Thanks MC
    >
    > inputFile = print("Enter Filename: ")
    > $stdout.flush
    > input = gets


    Try:

    input = gets.chomp

    gets is returning the \n at the end of the string.

    > myinput = input.to_s
    >
    > f = File.new(myinput) <-- in 'initialize': Invalid argument - filename
    > (Errno::EINAL) from crb.rb in 'new'


    Jesus.
    Jesús Gabriel y Galán, Apr 22, 2009
    #3
  4. Wow you seem to be a little confused her

    in line 1 you try to get a inputFile name from a print statement.

    You actually get the filename with line 3.

    line 4 is unnecessary as gets returns a string anyway.

    So you can do :

    puts 'Enter Filename: '
    $stdout.flush
    filename = gets.chomp

    inputfile = File.new(filename, 'w')

    Alan.

    On 22 Apr 2009, at 6:02 PM, Mmcolli00 Mom wrote:

    > inputFile = print("Enter Filename: ")
    > $stdout.flush
    > input = gets
    > myinput = input.to_s
    >
    > f = File.new(myinput) <-- in 'initialize': Invalid argument - filename
    > (Errno::EINAL) from crb.rb in 'new'
    Alan Claughan, Apr 22, 2009
    #4
  5. Oops that should be :

    puts 'Enter Filename: '
    $stdout.flush
    filename = gets.chomp

    inputfile = File.new(filename, 'r')

    :)



    On 22 Apr 2009, at 9:49 PM, Alan Claughan wrote:

    > Wow you seem to be a little confused her
    >
    > in line 1 you try to get a inputFile name from a print statement.
    >
    > You actually get the filename with line 3.
    >
    > line 4 is unnecessary as gets returns a string anyway.
    >
    > So you can do :
    >
    > puts 'Enter Filename: '
    > $stdout.flush
    > filename = gets.chomp
    >
    > inputfile = File.new(filename, 'w')
    >
    > Alan.
    >
    > On 22 Apr 2009, at 6:02 PM, Mmcolli00 Mom wrote:
    >
    >> inputFile = print("Enter Filename: ")
    >> $stdout.flush
    >> input = gets
    >> myinput = input.to_s
    >>
    >> f = File.new(myinput) <-- in 'initialize': Invalid argument -
    >> filename
    >> (Errno::EINAL) from crb.rb in 'new'

    >
    >
    Alan Claughan, Apr 22, 2009
    #5
  6. Alan Claughan wrote:
    > Oops that should be :
    >
    > puts 'Enter Filename: '
    > $stdout.flush
    > filename = gets.chomp
    >
    > inputfile = File.new(filename, 'r')
    >
    > :)


    thanks!
    --
    Posted via http://www.ruby-forum.com/.
    Mmcolli00 Mom, Apr 22, 2009
    #6
    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. chrisdude911
    Replies:
    10
    Views:
    135,374
    srk289
    Mar 10, 2009
  2. jefe
    Replies:
    2
    Views:
    1,105
    Victor Bazarov
    Apr 17, 2006
  3. gaurav kashyap
    Replies:
    2
    Views:
    613
    gaurav kashyap
    Oct 30, 2008
  4. gaurav kashyap
    Replies:
    3
    Views:
    683
    gaurav kashyap
    Oct 31, 2008
  5. Mel
    Replies:
    10
    Views:
    3,114
    Sailaja Appi
    Feb 13, 2009
Loading...

Share This Page