File Input and Output (I/O) Engineering 1D04, Teaching Session 7.
-
Upload
todd-percival-jackson -
Category
Documents
-
view
212 -
download
0
Transcript of File Input and Output (I/O) Engineering 1D04, Teaching Session 7.
![Page 1: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/1.jpg)
File Input and Output (I/O)
Engineering 1D04, Teaching Session 7
![Page 2: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/2.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 2
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.
![Page 3: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/3.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 3
File I/O
Problem: Find the distribution of grade points from 1D04 last year, i.e. How many people received each grade point?
![Page 4: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/4.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 4
0102030405060708090
100
0 2 4 6 8 10 12
Number ofStudents
Grade Point
File I/O
With this data, we should be able to produce a chart like this:
![Page 5: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/5.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 5
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.
![Page 6: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/6.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 6
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.
![Page 7: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/7.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 7
83868312910
grades.yyz
File I/O
Extract from the grade file
![Page 8: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/8.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 8
File I/O
Develop an algorithm to solve this problem?
![Page 9: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/9.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 9
0 1 2 3 4 5 6 7 8 9 10 11 12
array
indices
File I/O - Solution
We can use an array, with each index in the array representing a grade point.
![Page 10: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/10.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 10
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.
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 0 0 0 0 0 0 0 0 0 0
array
indices
Why?
File I/O - Solution
![Page 11: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/11.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 11
sequ
ence
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 0 0 0 0 0 1 0 0 0 0
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
![Page 12: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/12.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 12
sequ
ence
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 1 0 0 0 0 1 0 0 0 0
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.
![Page 13: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/13.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 13
sequ
ence
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 1 0 0 0 0 2 0 0 0 0
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.
![Page 14: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/14.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 14
sequ
ence
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 1 0 0 1 0 2 0 0 0 0
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.
![Page 15: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/15.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 15
sequ
ence
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 1 0 0 1 0 3 0 0 0 0
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.
![Page 16: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/16.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 16
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.
![Page 17: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/17.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 17
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.
![Page 18: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/18.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 18
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
![Page 19: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/19.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 19
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.
![Page 20: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/20.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 20
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
![Page 21: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/21.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 21
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.
![Page 22: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/22.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 22
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?
![Page 23: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/23.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 23
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)
![Page 24: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/24.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 24
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.
![Page 25: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/25.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 25
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.
![Page 26: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/26.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 26
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.
2
3
1
4
Why doesn't our C# loop look like this? Is there
another grade in the file?
Loop Visualization
![Page 27: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/27.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 27
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.
2
3
1
4
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
![Page 28: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/28.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 28
Loop VisualizationCreate 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?
2
3
1
4
We have to adjust the loop slightly to accommodate this new question.
Input one grade from the grade file.
3
![Page 29: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/29.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 29
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.
![Page 30: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/30.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 30
. . . int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt;. . .
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 0 0 0 0 0 0 0 0 0 0
File I/O - Animation
![Page 31: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/31.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 31
. . . int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt;. . . 8
3868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 0 0 0 0 0 0 0 0 0 0
File I/O - Animation
grades.yyz
![Page 32: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/32.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 32
. . . int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt;. . .
inputString
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 0 0 0 0 0 0 0 0 0 0
File I/O - Animation
grades.yyz
![Page 33: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/33.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 33
. . . int[] gradeDistribution = new int[13]; StreamReader inStream = new StreamReader("grades.yyz"); string inputString; int inputInt;. . .
inputString inputInt
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 0 0 0 0 0 0 0 0 0 0
File I/O - Animation
grades.yyz
![Page 34: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/34.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 34
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"8"
inputInt
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 0 0 0 0 0 0 0 0 0 0
File I/O - Animation
grades.yyz
![Page 35: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/35.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 35
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"8"
inputInt
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 0 0 0 0 0 0 0 0 0 0
True
File I/O - Animation
grades.yyz
![Page 36: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/36.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 36
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"8"
inputInt
8
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 0 0 0 0 0 0 0 0 0 0
File I/O - Animation
grades.yyz
![Page 37: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/37.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 37
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"8"
inputInt
8
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 0 0 0 0 0 1 0 0 0 0
File I/O - Animation
grades.yyz
![Page 38: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/38.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 38
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"3"
inputInt
8
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 0 0 0 0 0 1 0 0 0 0
File I/O - Animation
grades.yyz
![Page 39: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/39.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 39
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"3"
inputInt
8
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 0 0 0 0 0 1 0 0 0 0
True
File I/O - Animation
grades.yyz
![Page 40: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/40.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 40
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"3"
inputInt
3
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 0 0 0 0 0 1 0 0 0 0
File I/O - Animation
grades.yyz
![Page 41: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/41.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 41
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"3"
inputInt
3
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 1 0 0 0 0 1 0 0 0 0
File I/O - Animation
grades.yyz
![Page 42: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/42.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 42
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"1"
inputInt
1
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 0 0 2 0 0 1 0 3 1 0 0 1
Magically Skipping Steps
File I/O - Animation
grades.yyz
![Page 43: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/43.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 43
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"1"
inputInt
1
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 1 0 2 0 0 1 0 3 1 0 0 1
File I/O - Animation
grades.yyz
![Page 44: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/44.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 44
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"0"
inputInt
1
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 121 1 0 2 0 0 1 0 3 1 0 0 1
File I/O - Animation
grades.yyz
![Page 45: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/45.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 45
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"0"
inputInt
1
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 1 0 2 0 0 1 0 3 1 0 0 1
File I/O - Animation
grades.yyz
True
![Page 46: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/46.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 46
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"0"
inputInt
0
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 120 1 0 2 0 0 1 0 3 1 0 0 1
File I/O - Animation
grades.yyz
![Page 47: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/47.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 47
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"0"
inputInt
0
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 121 1 0 2 0 0 1 0 3 1 0 0 1
File I/O - Animation
grades.yyz
![Page 48: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/48.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 48
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
"0"
inputInt
0
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 121 1 0 2 0 0 1 0 3 1 0 0 1
File I/O - Animation
grades.yyz
![Page 49: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/49.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 49
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
null
inputInt
0
83868312910
0 1 2 3 4 5 6 7 8 9 10 11 121 1 0 2 0 0 1 0 3 1 0 0 1
File I/O - Animation
grades.yyz
![Page 50: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/50.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 50
inputString = inStream.ReadLine();while (inputString != null){ inputInt = Convert.ToInt32(inputString); gradeDistribution[inputInt]++; inputString = inStream.ReadLine();}. . .
inputString
null
inputInt
0
83868312910
False
0 1 2 3 4 5 6 7 8 9 10 11 121 1 0 2 0 0 1 0 3 1 0 0 1
File I/O - Animation
grades.yyz
![Page 51: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/51.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 51
. . . 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.
![Page 52: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/52.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 52
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.
![Page 53: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/53.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 53
Output to Data File
What information do I want to put in the file?
What format do I need to use?
![Page 54: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/54.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 54
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
![Page 55: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/55.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 55
Algorithm for Output to File
Run previously developed program. Start at beginning of grade point array Print a line in a file that reads:
arrayIndex arrayContents• 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.
![Page 56: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/56.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 56
. . 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:
arrayIndex arrayContents Loop through grade point array until all grade points have
been written to the file.
![Page 57: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/57.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 57
. . 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.
![Page 58: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/58.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 58
. . 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:
arrayIndex arrayContents Loop through grade point array until all grade points have
been written to the file.
![Page 59: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/59.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 59
. . 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:
arrayIndex arrayContents Loop through grade point array until all grade points have
been written to the file.
![Page 60: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/60.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 60
. . 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
![Page 61: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/61.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 61
. . 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.
![Page 62: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/62.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 62
0 11 12 03 24 05 06 17 08 39 110 011 012 1
Sample Output
Output from Sample Input:output.txt
![Page 63: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/63.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 63
Sample Output
If we open our file in Excel, the following message box will appear:
![Page 64: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/64.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 64
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.
![Page 65: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/65.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 65
0 11 12 03 24 05 06 17 08 39 110 011 012 1
Sample Output
And with a few clicks …
output.txt
![Page 66: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/66.jpg)
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 66
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.
![Page 67: File Input and Output (I/O) Engineering 1D04, Teaching Session 7.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f165503460f94c2cd33/html5/thumbnails/67.jpg)
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 Wassyng 67