G'day everyone.
Today I'll explain how to make a "Print File Contents" Script. Where it simple prints all the contents (The text) of what's in a file.
1. The Code
Apart from the obvious things, such as the gets.chomp, error handling and the basic Ruby Code, there is a code which you might not of seen before. This is the IO.read(file) code.
2. Explaining the Code
Now I don't really want to waste time with the #! /usr/bin/env ruby and basic things such as puts("Blah") or gets.chomp!, because you should know what they're if you've been reading my other tutorials. So I'll jump ahead to the Error Handling and IO.read code.
IO.read(file)
In the code, you would've seen something like:
Basically, this tells the program to read the file that the User has input, they input the directory at the beginning of the Program, which then is classified as the "file". So running the print file_contents = IO.read(file) will make it Output the Contents of the forementioned "file".
For example:
I ran the above script, and made it read a HTML document on my Desktop, here was the input / output:
Error Handling
As I've explained in most of my Tutorials, error handling is a must have if you plan on releasing your software / scripts that you make. It helps you as the Developer, and They as the Customers out alot.
For more explanation on the Error Handling code, please use my other tutorials as a guide. "File Writer" Tut has it explained in full.
Thanks for Reading and I hope I helped you out
-Jordan.
Today I'll explain how to make a "Print File Contents" Script. Where it simple prints all the contents (The text) of what's in a file.
1. The Code
Code:
############################################################################
# Copyright (C) 2009 by Pyrite Software Developers
#
#
# This program is free software; you can redistribute it and#or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
############################################################################
#! /usr/bin/env ruby
puts "Enter your File Directory you want to print [e.g. /home/~/Desktop/file.txt]: "
file = gets.chomp
puts("")
begin
print file_contents = IO.read(file)
puts ("")
puts "Success!"
rescue => e
puts "---> Failed To read file.\n\t|\n\t+---> #{e.class} #{e.message}"
end
2. Explaining the Code
Now I don't really want to waste time with the #! /usr/bin/env ruby and basic things such as puts("Blah") or gets.chomp!, because you should know what they're if you've been reading my other tutorials. So I'll jump ahead to the Error Handling and IO.read code.
IO.read(file)
In the code, you would've seen something like:
Code:
print file_contents = IO.read(file)
For example:
I ran the above script, and made it read a HTML document on my Desktop, here was the input / output:
Code:
jordan@ModernWarfare ~/Desktop/Ruby $ ruby reader.rb
Enter your File Directory you want to print [e.g. /home/~/Desktop/file.txt]:
/home/jordan/Desktop/index.html
<html>
<head>
<title>Sorry!</title>
</head>
<body>
<p> Sorry, this site is closed at the moment. Please try again later.
</o>
</body>
</html>
Success!
Error Handling
As I've explained in most of my Tutorials, error handling is a must have if you plan on releasing your software / scripts that you make. It helps you as the Developer, and They as the Customers out alot.
For more explanation on the Error Handling code, please use my other tutorials as a guide. "File Writer" Tut has it explained in full.
Thanks for Reading and I hope I helped you out
-Jordan.
At the top will be the same place you hang from.