Error when reading file from command prompt

M

Mmcolli00 Mom

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

Suresh Kk

Mmcolli00 said:
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
 
J

Jesús Gabriel y Galán

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

Alan Claughan

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

Alan Claughan

Oops that should be :

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

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

:)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top