# Error when reading file from command prompt

**URL:** <https://www.ruby-forum.com/t/error-when-reading-file-from-command-prompt/166196>\
**Category:** Ruby\
**Created:** [April 22, 2009, 6:02pm UTC](https://www.ruby-forum.com/t/error-when-reading-file-from-command-prompt/166196 "2009-04-22T18:02:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [April 22, 2009, 6:02pm UTC](https://www.ruby-forum.com/t/error-when-reading-file-from-command-prompt/166196/1 "2009-04-22T18:02:13Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [April 22, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/error-when-reading-file-from-command-prompt/166196/2 "2009-04-22T18:10:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [April 22, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/error-when-reading-file-from-command-prompt/166196/3 "2009-04-22T18:12:54Z")

</div>

On Wed, Apr 22, 2009 at 6:02 PM, Mmcolli00 Mom [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [April 22, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/error-when-reading-file-from-command-prompt/166196/4 "2009-04-22T21:50:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [April 22, 2009, 9:54pm UTC](https://www.ruby-forum.com/t/error-when-reading-file-from-command-prompt/166196/5 "2009-04-22T21:54:51Z")

</div>

Oops that should be :

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

inputfile = File.new(filename, ‘r’)

🙂

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [April 22, 2009, 11:02pm UTC](https://www.ruby-forum.com/t/error-when-reading-file-from-command-prompt/166196/6 "2009-04-22T23:02:41Z")

</div>

Alan Claughan wrote:

> Oops that should be :
> 
> puts 'Enter Filename: ’  
> $stdout.flush  
> filename = gets.chomp
> 
> inputfile = File.new(filename, ‘r’)
> 
> 🙂

thanks!
