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

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advanced Piloting Cruise Plot.
Kapitel 21 Astronomie Autor: Bennett et al. Galaxienentwicklung Kapitel 21 Galaxienentwicklung © Pearson Studium 2010 Folie: 1.
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 5 Author: Julia Richards and R. Scott Hawley.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Chapter 1 Image Slides Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
Determine Eligibility Chapter 4. Determine Eligibility 4-2 Objectives Search for Customer on database Enter application signed date and eligibility determination.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
Multiplying binomials You will have 20 seconds to answer each of the following multiplication problems. If you get hung up, go to the next problem when.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
Year 6 mental test 5 second questions
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
ZMQS ZMQS
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
Report Card P Only 4 files are exported in SAMS, but there are at least 7 tables could be exported in WebSAMS. Report Card P contains 4 functions: Extract,
Test on Input, Output, Processing, & Storage Devices
ABC Technology Project
1 Web-Enabled Decision Support Systems Access Introduction: Touring Access Prof. Name Position (123) University Name.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
© S Haughton more than 3?
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
© Charles van Marrewijk, An Introduction to Geographical Economics Brakman, Garretsen, and Van Marrewijk.
© Charles van Marrewijk, An Introduction to Geographical Economics Brakman, Garretsen, and Van Marrewijk.
VOORBLAD.
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
Squares and Square Root WALK. Solve each problem REVIEW:
Energy & Green Urbanism Markku Lappalainen Aalto University.
David Walker Ottawa TMG Users Group 15 March 2014.
© 2012 National Heart Foundation of Australia. Slide 2.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Test B, 100 Subtraction Facts
Week 1.
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Intracellular Compartments and Transport
1 Unit 1 Kinematics Chapter 1 Day
PSSA Preparation.
Essential Cell Biology
1 PART 1 ILLUSTRATION OF DOCUMENTS  Brief introduction to the documents contained in the envelope  Detailed clarification of the documents content.
Chapter 13 – Introduction to Classes
How Cells Obtain Energy from Food
Immunobiology: The Immune System in Health & Disease Sixth Edition
Chapter 30 Induction and Inductance In this chapter we will study the following topics: -Faraday’s law of induction -Lenz’s rule -Electric field induced.
Chapter 8 Improving the User Interface
Drill down Reconciliation Analysis Report (RFMFGRCN_RP1) in the Background Instructions Guide June, 2012.
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Presentation transcript:

CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (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 (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 (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 (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: ModeDescription rRead access only. Points to start of the file. This is the default, but it is considered good programming to specify it anyway. wWrite 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 (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 (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

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 Prints out the file (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 method file.puts(“text goes here.”) (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

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 file Closes file Instantiates a new file Outputs content of new file (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 (c) 2012 Ophir Frieder et al

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

Example 11.3 Cont’d 21 def close 23 end 24 end (c) 2012 Ophir Frieder et al

1 class FileReader 2 3 def initialize(file_name) = File.open(file_name, "r") 5 end 6 7 def read_file 8 whole_file = "" 9 while (input_line 10 whole_file += input_line 11 end return whole_file 14 end 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 file Display method which outputs the contents (c) 2012 Ophir Frieder et al

21 def close 23 end 24 end Method closes opened file (c) 2012 Ophir Frieder et al

Example 11.4: File Writer Class 1 class FileWriter 2 3 def initialize(file_name) = File.open(file_name, "w") 5 end 6 7 def write_line(output_line) 9 end def close 13 end 14 end (c) 2012 Ophir Frieder et al

1 class FileWriter 2 3 def initialize(file_name) = File.open(file_name, "w") 5 end 6 7 def write_line(output_line) 9 end def close 13 end 14 end Constructor Opens file with write access Write line method Outputs line to file Closes file (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

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 (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! (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. (c) 2012 Ophir Frieder et al