Upload
ronald-lawrence
View
212
Download
0
Embed Size (px)
Citation preview
March 27 - Files Big Idea: I will know how to create
software that can open and save to files Minds On: Learn how to learn Action: File Review Consolidation: Continue or move on?
Learn how to learn What strategies do you use to be
successful in school? How do you learn independently?
What strategies do you use to be successful in school?
Organization
In class/on time
Do your homework
Stay awake
Stay focused
Stay clean
How do you learn independently?
Research
Practice
Trial and error
Have initiative
Read a book
Youtube
Be responsibl
e
Time table plan
File Streams
D r e w 8 4 Cr Lf
T i a 9 2 Cr Lf
-1
//Example 3 System.IO.FileInfo textFile3 = new
System.IO.FileInfo("wonder.txt"); System.IO.StreamReader readFile; String lineOfText;
try { readFile = new
System.IO.StreamReader(textFile3.FullName); while ((lineOfText = readFile.ReadLine()) != null) { Console.WriteLine(lineOfText); } readFile.Close(); }
catch (Exception e) { Console.WriteLine("File does not exist
or could not be found."); Console.WriteLine(e.Message); }
YOUR TASK Create a console application called u3MMDDReadFileName
that reads and then displays the contents of a file containing instructions for this assignment. Use Notepad to create the file and be sure it is saved as a Text file (TXT). The application will need to include the correct path to the location. Bonus marks if you can figure out how to do it without a path!
AND ANOTHER TASK Create a program called u3MMDDReadNumbersName that
reads a text file that has a number on each line. As each line is read the number should be output and the number should be added to a variable called intTotal. Another variable intNumbOfItems is increased by one. Once the file is finished, then the average is calculated and displayed.
FileInfo dataFile = new FileInfo("scores.dat");
StreamReader readFile; String score; Double avgScore; Double totalScores = 0; Int32 numScores = 0;
try { readFile = new StreamReader(dataFile.FullName); while ((score = readFile.ReadLine()) != null) { numScores += 1; Console.WriteLine(score); totalScores += Double.Parse(score); } avgScore = totalScores / numScores; Console.WriteLine("Average = " + avgScore); readFile.Close(); }
catch(Exception ex){ Console.WriteLine("The following error
occurred: "); Console.WriteLine(ex.Message); }