File Input and Output (I/O) Engineering 1D04, Teaching Session 7.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Visual Studio 2010 and Controls.
Advertisements

CS102--Object Oriented Programming
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
PowerPoint: Tables Computer Information Technology Section 5-11 Some text and examples used with permission from: Note: We are.
Line up By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
CS0007: Introduction to Computer Programming File IO and Recursion.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 12: How Long Can This Go On?
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Arrays, Type Casting and Constants Engineering 1D04, Teaching Session 5.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Introduction to Algorithms Engineering 1D04, Teaching Session 1.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
File I/O ifstreams and ofstreams Sections 11.1 &
Records Engineering 1D04, Teaching Session 11. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng1 Records  So far we've seen a number of different.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Visual Basic Programming
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
INPUT AND OUTPUT 1. OUTPUT TextBoxes & Labels MsgBox Function/Method MessageBox Class Files (next topic) Printing (later) 2.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Computers, Variables and Types Engineering 1D04, Teaching Session 2.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
The Professional Touch Programming Right from the Start with Visual Basic.NET 1/e 10.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Objects and Classes Continued Engineering 1D04, Teaching Session 10.
Tutorial for Arrays and Lists. Description This presentation will cover the basics of using Arrays and Lists in an Alice world It uses a set of chickens.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Strings Chapter 5.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.
Objects and Classes Engineering 1D04, Teaching Session 9.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
File I/O. Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
NXT File System Just like we’re able to store multiple programs and sound files to the NXT, we can store text files that contain information we specify.
Two Dimensional Arrays and Complex Conditions
Files.
Engineering Innovation Center
MATLAB: Structures and File I/O
Sequential Input and Output using Text Files
File Input/Output (I/O)
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Functions continued.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Presentation transcript:

File Input and Output (I/O) Engineering 1D04, Teaching Session 7

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng1 using System.IO; File I/O  For anything in this session to work correctly, you need to include the File I/O Library.  This can be done by adding the following line at the top of your C# program with the rest of the using statements.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng2 File I/O  Problem: Find the distribution of grade points from 1D04 last year, i.e. How many people received each grade point?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng3 Grade Point File I/O  With this data, we should be able to produce a chart like this:

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng4 File I/O  There were 800 people in 1D04 last year. This is a lot of data to enter manually - if we have the grades already stored in a computer readable form.  When large amounts of data need to be used in a computer program it is essential to be able to import this data from a file.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng5 File I/O  In this example, a sample file has been prepared called grades.yyz.  Each line has a number from 0-12 corresponding to the McMaster Grade Scale.  Student names are not included.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng grades.yyz File I/O  Extract from the grade file

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng7 File I/O  Develop an algorithm to solve this problem?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng array indices File I/O - Solution  We can use an array, with each index in the array representing a grade point.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng9  Each location will represent how many people scored that grade.  We need to zero each location as we have not read in any grades yet array indices Why? File I/O - Solution

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng10 sequence index Increment Value by 1 File I/O - Solution  As we read through the file of grades, we will increment the value stored at the location of the corresponding grade point. That is why we zero the elements first

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng11 sequence index Increment Value by 1 File I/O - Solution  As we read through the file of grades, we will increment the value stored at the location of the corresponding grade point.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng12 sequence index Increment Value by 1 File I/O - Solution  As we read through the file of grades, we will increment the value stored at the location of the corresponding grade point.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng13 sequence index Increment Value by 1 File I/O - Solution  As we read through the file of grades, we will increment the value stored at the location of the corresponding grade point.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng14 sequence index Increment Value by 1 File I/O - Solution  As we read through the file of grades, we will increment the value stored at the location of the corresponding grade point.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng15 File I/O - Algorithm  Create and zero an array of 13 elements for grade points.  Input one grade from the grade file.  Increment the respective location in the grade point array.  Repeat until all grades have been used.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng16 void findGradeDistribution(){ int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt; inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); } inStream.Close(); } File I/O - Declarations  Streams are used to work with files.  A stream is simply a name for a flow of data.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng17 void findGradeDistribution(){ int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt; inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); } inStream.Close(); } File I/O - Declarations  Streams are used to work with files.  A stream is simply a name for a flow of data. This opens the stream for reading name of stream

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng18 void findGradeDistribution(){ int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt; inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); } inStream.Close(); } File I/O - Declarations  The filename associated with the stream is a string passed as a parameter inside ()’s when creating a new stream.  To make it easier right now, the file needs to be in the working directory for your program.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng19 File I/O - Declarations  In Visual Studio, the working directory for an application is:  projectFolder\ProjectName\bin\Debug  Example:  C:\Documents and Settings\username\ My Documents\Visual Studio 2005\Projects\ gradeThingy\gradeThingy\bin\Debug

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng20 void findGradeDistribution(){ int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt; inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); } inStream.Close(); } File I/O  Create and zero an array of 13 elements for grade points.  Input one grade from the grade file.  Increment the respective location in the grade point array.  Repeat until there are no more grades in the file.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng21 void findGradeDistribution(){ int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt; inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); } inStream.Close(); } File I/O  Create and zero an array of 13 elements for grade points.  Input one grade from the grade file.  Increment the respective location in the grade point array.  Repeat until there are no more grades in the file. why?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng22 void findGradeDistribution(){ int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt; inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); } inStream.Close(); } File I/O  Create and zero an array of 13 elements for grade points.  Input one grade from the grade file.  Increment the respective location in the grade point array.  Repeat until there are no more grades in the file. why? assumption (at least 1)

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng23 void findGradeDistribution(){ int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt; inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); } inStream.Close(); } File I/O  Create and zero an array of 13 elements for grade points.  Input one grade from the grade file.  Increment the respective location in the grade point array.  Repeat until there are no more grades in the file.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng24 void findGradeDistribution(){ int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt; inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); } inStream.Close(); } File I/O  Create and zero an array of 13 elements for grade points.  Input one grade from the grade file.  Increment the respective location in the grade point array.  Repeat until there are no more grades in the file.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng25 Create and zero an array of 13 elements for grade points. Input one grade from the grade file. Increment the respective location in the grade point array Why doesn't our C# loop look like this? Is there another grade in the file? Loop Visualization

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng26 Create and zero an array of 13 elements for grade points. Input one grade from the grade file. Increment the respective location in the grade point array In C#, we can't ask the question Is there another grade in the file? Instead, we can ask the question Does the last grade we attempted to read, exist? Is there another grade in the file? Loop Visualization

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng27 Loop Visualization Create and zero an array of 13 elements for grade points. Input one grade from the grade file. Increment the respective location in the grade point array. Does the last grade we attempted to read, exist? We have to adjust the loop slightly to accommodate this new question. Input one grade from the grade file. 3

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng28 void findGradeDistribution(){ int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt; inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); } inStream.Close(); } File I/O  inStream.ReadLine() is where all the magic happens.  inStream.ReadLine() returns a string value of the next unread line unless there are no more unread lines in which case it returns null.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng29... int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt; File I/O - Animation

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng30... int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt; File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng31... int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt;... inputString File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng32... int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt;... inputStringinputInt File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng33 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "8" inputInt File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng34 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "8" inputInt True File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng35 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "8" inputInt File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng36 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "8" inputInt File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng37 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "3" inputInt File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng38 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "3" inputInt True File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng39 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "3" inputInt File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng40 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "3" inputInt File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng41 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "1" inputInt Magically Skipping Steps File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng42 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "1" inputInt File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng43 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "0" inputInt File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng44 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "0" inputInt File I/O - Animation grades.yyz True

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng45 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "0" inputInt File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng46 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "0" inputInt File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng47 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString "0" inputInt File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng48 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString null inputInt File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng49 inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); }... inputString null inputInt False File I/O - Animation grades.yyz

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng50... inputString = inStream.ReadLine(); while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine(); } inStream.Close(); } File I/O  Streams always need to be closed when they are finished being used.  If they don’t get closed, unexpected results may appear in the files that are being accessed.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng51 Output to Data File  Now that our program knows the distribution of grades from our input file, it would be nice to produce a graph.  Graphs are not simple to produce.  However, we can easily output our data to a file and open it in Excel. With just a few clicks in Excel, a graph can be created.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng52 Output to Data File  What information do I want to put in the file?  What format do I need to use?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng53 Output to Data File  What information do I want to put in the file?  The grade points  The number of students with each grade point.  What format do I need to use?  A file that is tab delimited is easily accessed in Excel. In this case, tab delimited means that the tab character separates entries on any line, i.e. grade_point_average tab number_of_students

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng54 Algorithm for Output to File  Run previously developed program.  Start at beginning of grade point array  Print a line in a file that reads:  arrayIndexarrayContents Where arrayIndex is the index of grade point array. arrayContents is the numerical value in that index. arrayIndex and arrayContents are separated by a tab.  Loop through grade point array until all grade points have been written to the file.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng55.. Program to input / create data not shown on this slide.. StreamWriter outStream = new StreamWriter("output.txt"); for (int i = 0; i < gradeDistribution.Length; i++){ outStream.WriteLine(i + "\t" + gradeDistribution[i]); } outStream.Close(); Output to Data File  Run previously developed program.  Start at beginning of grade point array.  Print a line in a file that reads:  arrayIndexarrayContents  Loop through grade point array until all grade points have been written to the file.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng56.. Program to input / create data not shown on this slide.. StreamWriter outStream = new StreamWriter("output.txt"); for (int i = 0; i < gradeDistribution.Length; i++){ outStream.WriteLine(i + "\t" + gradeDistribution[i]); } outStream.Close(); Output to Data File  This declares an output stream.  It creates a file called "output.txt" in the application's working directory.  We can write strings to this output stream in the same way we have used strings in message boxes for example.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng57.. Program to input / create data not shown on this slide.. StreamWriter outStream = new StreamWriter("output.txt"); for (int i = 0; i < gradeDistribution.Length; i++){ outStream.WriteLine(i + "\t" + gradeDistribution[i]); } outStream.Close(); Output to Data File  Run previously developed program.  Start at beginning of grade point array.  Print a line in a file that reads:  arrayIndexarrayContents  Loop through grade point array until all grade points have been written to the file.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng58.. Program to input / create data not shown on this slide.. StreamWriter outStream = new StreamWriter("output.txt"); for (int i = 0; i < gradeDistribution.Length; i++){ outStream.WriteLine(i + "\t" + gradeDistribution[i]); } outStream.Close(); Output to Data File  Run previously developed program.  Start at beginning of grade point array.  Print a line in a file that reads:  arrayIndexarrayContents  Loop through grade point array until all grade points have been written to the file.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng59.. Program to input / create data not shown on this slide.. StreamWriter outStream = new StreamWriter("output.txt"); for (int i = 0; i < gradeDistribution.Length; i++){ outStream.WriteLine(i + "\t" + gradeDistribution[i]); } outStream.Close(); Output to Data File  WriteLine() does all the magic of writing to a file.  It writes a single string to our output file (ends with "\n").  \t is the code for a tab.  "\t" is a string with a tab in it

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng60.. Program to input / create data not shown on this slide.. StreamWriter outStream = new StreamWriter("output.txt"); for (int i = 0; i < gradeDistribution.Length; i++){ outStream.WriteLine(i + "\t" + gradeDistribution[i]); } outStream.Close(); Output to Data File  Streams must be closed when we are finished using them.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng Sample Output  Output from Sample Input: output.txt

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng62 Sample Output  If we open our file in Excel, the following message box will appear:

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng63 Sample Output  Excel starts this wizard if plain text files are opened with Excel.  Because of our explicit delimiting of our file with tabs, we can just click finish.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng Sample Output And with a few clicks … output.txt

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng65 File I/O  The brilliant part of file I/O like this is that it doesn’t matter how big or small our input file is.  The software will read through the entire file and create sensible output.

File Dialog Boxes  Easy to include the normal Windows dialogs for browsing to a specific location to store or retrieve a file  Can add OpenFileDialog or SaveFileDialog by dragging from Toolbox to the design view onto your form  The ShowDialog() method shows the dialog  The FileName property gives the filename © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng66