chapter 11 file input & output introduction to computer science using ruby (c) 2012 ophir...

22
CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

Upload: alessandro-soto

Post on 01-Apr-2015

218 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

CHAPTER 11 FILE INPUT & OUTPUTIntroduction to Computer Science Using Ruby

Page 2: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

File Access: Reading & Writing Files can be

manipulated using File Input/Output (I/O)

To access files, the built-in Ruby File Class is used

File contains multiple methods: open close gets puts

Page 3: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

File Access: Reading & Writing

The method File.open instantiates a new file object Enables Ruby to read from or

write to an existing or new file The file object returned by

File.open can then be used to access the file specified in the File.open statement

Page 4: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

File Access: Reading & Writing The following code shows how to open a

file:my_file = File.open(file_name,access_mode)

The my_file variable is a File object that can now be used to interact with the file’s contents

The file_name is the name of the file in the system – it is highly system dependant

Page 5: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

File Access: Reading & Writing The way my_file functions depends on

what access mode is used. The two main modes are:

Mode Description

r Read access only. Points to start of the file. This is the default, but it is considered good programming to specify it anyway.

w Write access only. Points to the beginning of the file which will overwrite the file’s contents if it already exists.Table 11.1: Some Access Modes

Page 6: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

File Access: Reading & Writing

To read a line of characters from a file, call the gets method gets will read a new line each

time Returns nil when it reaches the

end of the file

Page 7: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

Example 11.1: Sample Code for File Reading

1 file = File.open("foo.txt", "r") 2 whole_file = "" 3 4 while (input_line = file.gets) 5 whole_file += input_line 6 end 7 8 puts "Contents of input file:" 9 puts whole_file

Note: Reads and prints the “foo.txt.” file

(c) 2012 Ophir Frieder et al

Page 8: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

1 file = File.open("foo.txt", "r") 2 whole_file = "" 3 4 while (input_line = file.gets) 5 whole_file += input_line 6 end 7 8 puts "Contents of input file:" 9 puts whole_file

Opens the file for read

access

Will store the file to display

Will store the file to display

Prints out the

file

Page 9: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

File Access: Reading & Writing Writing to a file is similar to reading from

a file Instead of using read mode, use “w” for

writing access To output text to a file, use a local

variable and invoke the puts methodfile.puts(“text goes here.”)

Page 10: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

Example 11.2: File Read/Write Example

This code writes input into a file:

1 file_a = File.open("bar.txt", "w") 2 3 puts "Please enter a line of text" 4 line = gets 5 file_a.puts(line) 6 file_a.close 7 8 file_b = File.open("bar.txt", "r") 9 puts "Contents of file:" 10 puts file_b.gets

(c) 2012 Ophir Frieder et al

Page 11: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

1 file_a = File.open("bar.txt", "w") 2 3 puts "Please enter a line of text" 4 line = gets 5 file_a.puts(line) 6 file_a.close 7 8 file_b = File.open("bar.txt", "r") 9 puts "Contents of file:" 10 puts file_b.gets

Opens file with write

access

Takes text input from the user

Writes user

input to fileCloses

fileInstantiates a new

file

Outputs content of new file

Page 12: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

File Reader Class

We can now define a class which encapsulates file reading

The next example encapsulates reading and displaying a file

Page 13: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

Example 11.3: File Reader Class

1 class FileReader 2 3 def initialize(file_name) 4 @file = File.open(file_name, "r") 5 end 6 7 def read_file 8 whole_file = "" 9 while (input_line = @file.gets) 10 whole_file += input_line 11 end 12 13 return whole_file 14 end 15 16 def display 17 puts "Contents of input file:" 18 puts read_file 19 end 20 (c) 2012 Ophir Frieder et al

Page 14: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

Example 11.3 Cont’d

21 def close 22 @file.close 23 end 24 end

Page 15: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

1 class FileReader 2 3 def initialize(file_name) 4 @file = File.open(file_name, "r") 5 end 6 7 def read_file 8 whole_file = "" 9 while (input_line = @file.gets) 10 whole_file += input_line 11 end 12 13 return whole_file 14 end 15 16 def display 17 puts "Contents of input file:" 18 puts read_file 19 end 20

Defines the

constructor

Opens the file to read contents

Defines the

method

Basic loop reads file

one line at a time Appends

contents to whole_file

Returns contents of

fileDisplay method which

outputs the contents

(c) 2012 Ophir Frieder et al

Page 16: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

21 def close 22 @file.close 23 end 24 end

Method closes

opened file

Page 17: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

Example 11.4: File Writer Class

1 class FileWriter 2 3 def initialize(file_name) 4 @file = File.open(file_name, "w") 5 end 6 7 def write_line(output_line) 8 @file.puts(output_line) 9 end 10 11 def close 12 @file.close 13 end 14 end

(c) 2012 Ophir Frieder et al

Page 18: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

1 class FileWriter 2 3 def initialize(file_name) 4 @file = File.open(file_name, "w") 5 end 6 7 def write_line(output_line) 8 @file.puts(output_line) 9 end 10 11 def close 12 @file.close 13 end 14 end

Constructor

Opens file with write

access

Write line methodOutputs

line to file

Closes file

Page 19: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

Example 11.5: FileReader/FileWriter Example

1 require_relative "file_reader.rb" 2 require_relative "file_writer.rb" 3 4 fr = FileReader.new("input.txt") 5 fw = FileWriter.new("output.txt") 6 7 input = fr.read_file 8 fw.write_line(input) 9 10 fw.close 11 fr.close

This example uses the FileReader and FileWriter classes to read a file, then writes its contents to another file

(c) 2012 Ophir Frieder et al

Page 20: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

1 require_relative "file_reader.rb" 2 require_relative "file_writer.rb" 3 4 fr = FileReader.new("input.txt") 5 fw = FileWriter.new("output.txt") 6 7 input = fr.read_file 8 fw.write_line(input) 9 10 fw.close 11 fr.close

Imports classes

Instances for the

classes

Reads the file

Writes string to the file

Closes the file

Page 21: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

File Reader/Writer Example

Test the program using Example 11.6: Sample Input File

Hello World!A mighty fine day for ruby programming!Computer Science is the best!

Page 22: CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

(c) 2012 Ophir Frieder et al

Summary

We described the basic file input and output operations

Note that Ruby allows other file operations that are not covered in these lectures.