Reading input from text file

i want my ruby program to accept input from text file as parameter at
command prompt and produce the and save it another text.

i have all my input for my program at inputfile.txt.

i want my program to work if run prorgram like

ruby filename.rb inputfile.txt > outputfile.txt

thanks in advance

On Wed, Feb 12, 2014 at 5:45 PM, Sundar S. [email protected]
wrote:

i want my ruby program to accept input from text file as parameter at
command prompt and produce the and save it another text.

i have all my input for my program at inputfile.txt.

You can use the ARGF object to read input from files.

i want my program to work if run prorgram like

ruby filename.rb inputfile.txt > outputfile.txt

For the output, if the above is what you want, you just need to print
to stdout (puts, print, etc), cause you are redirecting to the file at
the shell (> outputfile.txt)

For example:

file_contents = ARGF.read

do something with file_contents

puts file_contents

Now:

$ ruby test_argf.rb test.txt > result.txt
cat result.txt
aaaaa
bbbbb
ccccc

Hope this helps,

Jesus.

I don’t think I can make it.

Sent from my iPad

input_file_name = ARGV[0]

output_file_name = ARGV[1]

You’ll need to do some error checking in case one or both aren’t
entered. For the output, you’ll need to write it out with something like
this:

File.open(my_output, ‘w+’) {|f| f.write(output_file_name)}

If you want to use options, look at the option parser built into Ruby
(optparse).

http://ruby-doc.org/stdlib-2.1.0/libdoc/optparse/rdoc/OptionParser.html

Wayne


From: Sundar S. [email protected]
To: [email protected]
Sent: Wednesday, February 12, 2014 10:45 AM
Subject: reading input from text file

i want my ruby program to accept input from text file as parameter at
command prompt and produce the and save it another text.

i have all my input for my program at inputfile.txt.

i want my program to work if run prorgram like

ruby filename.rb inputfile.txt > outputfile.txt

thanks in advance

Am 12.02.2014 18:41, schrieb Wayne B.:

input_file_name = ARGV[0]
output_file_name = ARGV[1]

Note that this does not correspond to the OP’s intended usage
of the script, though:

ruby filename.rb inputfile.txt > outputfile.txt

which expects one argument only and pipes the output to a file.

Regards,
Marcus

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs