Thread Rating:
  • 3 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Printing the File Contents
#1
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

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
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:
Code:
print file_contents = IO.read(file)
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:
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 Smile
-Jordan.
At the top will be the same place you hang from.
Reply
#2
I am not familiar in printing the file contents. In fact, your tutorial would be a great help for it. I am actually conscious before on how to print file contents, but fortunately, it was just simple then.
Reply
#3
Thanks for the tutorial mate. It will be useful for many people.
That's really weird,Ninja
Reply
#4
Great tutorial, should come in handy for some people.
Reply
#5
Thanks this is very helpful Smile
Reply
#6
Thanks for the TUT.
Reply
#7
Good job Thanks
Reply
#8
Thanks for the usefull tutorial. Really well explained, thanks!
Reply
#9
Great tutorial,
very useful for me . thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Making a Write-To-File Script Jordan L. 7 2,801 01-14-2010, 03:40 AM
Last Post: Jordan L.
  [Tut] How to make a File Creator with Ruby Jordan L. 4 1,438 01-14-2010, 02:53 AM
Last Post: Jordan L.

Forum Jump:


Users browsing this thread: 1 Guest(s)