Download presentation
Presentation is loading. Please wait.
Published byRonald Lawrence Modified over 9 years ago
1
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?
2
Learn how to learn What strategies do you use to be successful in school? How do you learn independently?
3
What strategies do you use to be successful in school? Organization In class/on time Do your homework Stay awakeStay focusedStay clean
4
How do you learn independently? ResearchPractice Trial and error Have initiative Read a book Youtube Be responsible Time table plan
5
File Streams Drew 84CrLfTia 92CrLf Drew 84CrLfTia 92CrLf
6
//Example 3 System.IO.FileInfo textFile3 = new System.IO.FileInfo("wonder.txt"); System.IO.StreamReader readFile; String lineOfText;
7
try { readFile = new System.IO.StreamReader(textFile3.FullName); while ((lineOfText = readFile.ReadLine()) != null) { Console.WriteLine(lineOfText); } readFile.Close(); }
8
catch (Exception e) { Console.WriteLine("File does not exist or could not be found."); Console.WriteLine(e.Message); }
9
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.
10
FileInfo dataFile = new FileInfo("scores.dat"); StreamReader readFile; String score; Double avgScore; Double totalScores = 0; Int32 numScores = 0;
11
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(); }
12
catch(Exception ex){ Console.WriteLine("The following error occurred: "); Console.WriteLine(ex.Message); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.