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

···

--
Posted via http://www.ruby-forum.com/.

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 Shan <lists@ruby-forum.com>
To: ruby-talk@ruby-lang.org
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

--
Posted via http://www.ruby-forum.com/.

I don't think I can make it.

···

Sent from my iPad

On Feb 12, 2014, at 12:42 PM, "Wayne Brisette" <wbrisett@att.net> wrote:

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

Class: OptionParser (Ruby 2.1.0)

Wayne

From: Sundar Shan <lists@ruby-forum.com>
To: ruby-talk@ruby-lang.org
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

--
Posted via http://www.ruby-forum.com/\.

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.

···

On Wed, Feb 12, 2014 at 5:45 PM, Sundar Shan <lists@ruby-forum.com> wrote:

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

···

Am 12.02.2014 18:41, schrieb Wayne Brisette:

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A