C File IO Line by line and tokenbased
C# File I/O Line by line and token-based file input Copyright 2008 by Pearson Education
Input/output (I/O) using System. IO; Create a File. Info object to get info about a file on disk. (This doesn't actually create a new file on the hard disk. ) File. Info f = new File. Info(“Files\example. txt"); if (f. Exists && f. Length > 1000) { f. Delete(); } Method name Description Delete() removes file from disk Move. To(filename) moves and/or changes name of file Property name Description Exists whether this file exists on disk Name returns file's name Length returns number of bytes in file Copyright 2008 by Pearson Education 2
Type of Files Binary files http: //en. wikipedia. org/wiki/Binary_file Can contain arbitrary patterns of bits (data) We will NOT be dealing with these Include program. EXE's, . CLASS files, image files (PNG, GIF, JPEG, etc), video files, etc. Text Files http: //en. wikipedia. org/wiki/Text_file Have ONLY text inside them Includes anything you made using Notepad, source code files (i. e. , files ending in. cs, . java, . cpp), HTML web pages (. html), XML, most-if-not-all Unix/Linux configuration files, etc Copyright 2008 by Pearson Education 3
Reading files To read a file, call Open. Text on a File. Info object. File. Info file = new File. Info("Files\example. txt"); Text. Reader name = file. Open. Text(); or, even better: Text. Reader name = new Stream. Reader("Files\example. txt"); Copyright 2008 by Pearson Education 4
Closing files You will need to dispose of the file when you’re done with it: Text. Reader name = new Stream. Reader("Files\example. txt"); // The program reads the file here name. Dispose(); C# provides using, which will call dispose for you: using( Text. Reader name = new Stream. Reader(@"Filesexample. txt") { // The program reads the file here } ) name. Dispose() called automatically on exit, no matter how you exit (reach the closing }, return statement, throw an exception, etc) Copyright 2008 by Pearson Education 5
File paths absolute path: specifies a drive or a top "/" folder C: /Documents/smith/hw 6/input/data. csv Windows can also use backslashes to separate folders. relative path: does not specify any top-level folder names. dat input/kinglear. txt Assumed to be relative to the current directory: File. Info file = new File. Info("data/readme. txt"); If our program is in H: /hw 6, File. Info will look for H: /hw 6/data/readme. txt You can use . . to move up a folder level: with. . /hw 3/data/readme. txt File. Info will look for H: /hw 3/data/readme. txt Copyright 2008 by Pearson Education 6
Possible error w/ files The following program crashes when run: public void Slide_07() { using (Text. Reader t = new Stream. Reader("missing_file. txt") ) { string s = t. Read. Line(); } } The following error occurs: Unhandled Exception: System. IO. File. Not. Found. Exception: Could not find file 'E: pathtoprogrammissing_file. txt'. File name: 'E: pathtoprogrammissing_file. txt' at System. IO. __Error. Win. IOError(Int 32 error. Code, String maybe. Full. Path) < snip – extra stuff removed for clarity > at PCE_Starter. Project. Slide. Examples. Slide_05() in E: pathtoprogramStudent_Answers. cs: line 76 at PCE_Starter. Project. Program. Main(String[] args) in E: pathtoprogramStudent_Answers. cs: line 30 Copyright 2008 by Pearson Education 7
Exceptions exception: An object representing a runtime error. dividing an integer by 0 calling char. At on a String and passing too large an index trying to read a file that does not exist We say that a program with an error "throws" an exception. It is also possible to "catch" (handle or fix) an exception. We will not be catching any exceptions in this topic Copyright 2008 by Pearson Education 8
Input tokens token: A unit of user input, separated by whitespace. We can split a file's contents into tokens. If an input file contains the following: 23 3. 14 "John Smith" We can interpret the tokens as the following types: Token 23 3. 14 "John Smith" Copyright 2008 by Pearson Education Type(s) int, double, String 9
Line-Based File Parsing Consider a file numbers. txt that contains this text: (Note that ˽ means a blank space, t means a tab) 308. 2 ˽˽˽˽ 7. 4 3. 9˽˽˽ ˽-15. 4˽˽˽ We can think of the file's contents like so: 308. 2n˽˽˽˽ 7. 4n 3. 9˽˽˽nt˽-15. 4˽˽˽nn We can get each line (using the Text. Reader. Read. Line()), then deal with each line individually Copyright 2008 by Pearson Education 10
File input question Recall the input file numbers. txt: 308. 2 ˽˽˽˽ 7. 4 3. 9˽˽˽ t˽-15. 4˽˽˽ Write a program that reads the lines from the file and prints each one followed by a < sign (so it's clear where the line ends)(note the blank line at the end of the output) 308. 2< 7. 4< 3. 9 < -15. 4 < < Copyright 2008 by Pearson Education 11
File Input Answer public void Slide_12() { using (Text. Reader file = new Stream. Reader("Files/numbers. txt")) { for (int i = 0; i < 5; i++) { string s. Line = file. Read. Line(); Console. Write. Line("{0}<", s. Line ); } } } Copyright 2008 by Pearson Education 12
File input question Recall the input file numbers. txt: 308. 2 ˽˽˽˽ 7. 4 3. 9˽˽˽ t˽-15. 4˽˽˽ Write a program that reads the values from the file and prints them along with their sum. number = 308. 2 number = 7. 4 number = 3. 9 number = -15. 4 Sum = 304. 1 Copyright 2008 by Pearson Education 13
Testing for valid input All basic data types offer a version of Try. Parse: Method Int 32. Try. Parse Double. Try. Parse Description http: //msdn. microsoft. com/enus/library/f 02979 c 7. aspx http: //msdn. microsoft. com/enus/library/994 c 0 zb 1. aspx short. Try. Parse aka Int 16. Try. Parse http: //msdn. microsoft. com/enus/library/9 hh 1 awhy. aspx If given a valid string (Int 32, Double, short, etc) each one will change it's second parameter to be that value and return true If given an invalid string (Int 32, Double, short, etc) each one will change it's second parameter to be zero and return false Copyright 2008 by Pearson Education 14
File Input Answer public void Slide_15() { using (Text. Reader file = new Stream. Reader("Files/numbers. txt")) { double sum = 0. 0; for( int i = 0; i < 5; i++) { string s. Line = file. Read. Line(); double d. Num; if (Double. Try. Parse(s. Line, out d. Num)) { sum += d. Num; Console. Write. Line("number = {0}", d. Num); } } Console. Write. Line("Sum = {0}", sum); } } Copyright 2008 by Pearson Education 15
Reading an entire file Suppose we want our program to process the entire file. (It should work no matter how many values are in the file. ) number = 308. 2 number = 7. 4 number = 3. 9 number = -15. 4 number = 4. 7 number = 5. 4 number = 2. 8 Sum = 317 Copyright 2008 by Pearson Education 308. 2 ˽˽˽˽ 7. 4 3. 9˽˽˽ t˽-15. 4˽˽˽ 4. 7 ˽ 5. 4 2. 8 16
Reading an entire file answer public void Slide_17() { using (Text. Reader file = new Stream. Reader("Files/numbers 2. txt")) { double sum = 0. 0; string s. Line; s. Line = file. Read. Line(); while (s. Line != null) { double d. Num; if (Double. Try. Parse(s. Line, out d. Num)) { sum += d. Num; Console. Write. Line("number = {0}", d. Num); } s. Line = file. Read. Line(); } Console. Write. Line("Sum = {0}", sum); } } Copyright 2008 by Pearson Education 17
File input question 3 Test the program to make sure that it handles files that contain non-numeric tokens (by skipping them). For example, it should produce the same output as before when given this input file, numbers 3. txt: 308. 2 hello oops 7. 4 bad 3. 9 stuff -15. 4 4. 7 5. 4 : -) 2. 8 @#*($& Copyright 2008 by Pearson Education 308. 2˽hello ˽˽˽˽oops˽ 7. 4 bad˽ 3. 9˽stuff -15. 4 4. 7 5. 4 : -)˽ 2. 8˽˽@#*($& 18
File input answer 3 public void Slide_19() { char[] delimiters = { ' ', 't' }; using (Text. Reader file = new Stream. Reader("Files/numbers 3. txt")) { double sum = 0. 0; string s. Line = file. Read. Line(); while (s. Line != null) { string[] tokens. From. Line = s. Line. Split(delimiters, String. Split. Options. Remove. Empty. Entries); foreach (string token in tokens. From. Line) { double d. Num; if (Double. Try. Parse(token, out d. Num)) { sum += d. Num; Console. Write. Line("number = {0}", d. Num); break; // out of foreach } } s. Line = file. Read. Line(); } Console. Write. Line("Sum = {0}", sum); } } Copyright 2008 by Pearson Education 19
Election question Write a program that reads a file poll. txt of poll data. Format: State Obama% Mc. Cain% Electoral. Votes Pollster CT 56 31 7 Oct U. of Connecticut NE 37 56 5 Sep Rasmussen AZ 41 49 10 Oct Northern Arizona U. The program should print how many electoral votes each candidate leads in, and who is leading overall in the polls. Obama: 7 votes Mc. Cain: 15 votes Overall: Mc. Cain Copyright 2008 by Pearson Education 20
Election answer public void Slide_21() { char[] delimiters = { ' ', 't' }; int obama. Votes = 0, mccain. Votes = 0, e. Votes = 0; using (Text. Reader t = new Stream. Reader("Files/poll. txt")) { string s. Line = t. Read. Line(); while (s. Line != null) { string[] tokens = s. Line. Split(delimiters, String. Split. Options. Remove. Empty. Entries); if (tokens. Length < 5) { Console. Write. Line("Did not find the expected number of items on line: nt{0}", s. Line); s. Line = t. Read. Line(); continue; } Copyright 2008 by Pearson Education 21
Election answer int obama; if (!Int 32. Try. Parse(tokens[1], out obama)) { Console. Write. Line("Obama's votes not formatted properly on linent{0}", s. Line); s. Line = t. Read. Line(); continue; } int mccain; if (!Int 32. Try. Parse(tokens[2], out mccain)) { Console. Write. Line("Mc. Cain's votes not formatted properly on linent{0}", s. Line); s. Line = t. Read. Line(); continue; } if (!Int 32. Try. Parse(tokens[3], out e. Votes)) { Console. Write. Line("Electoral votes not formatted properly on linent{0}", s. Line); s. Line = t. Read. Line(); continue; } Copyright 2008 by Pearson Education 22
Election answer if (obama > mccain) obama. Votes += e. Votes; else if (mccain > obama) mccain. Votes += e. Votes; // on tie neither candidate gets the votes s. Line = t. Read. Line(); } // end of while } Console. Write. Line("Obama: {0} votes", obama. Votes); Console. Write. Line("Mc. Cain: {0} votes", mccain. Votes); if (obama. Votes > mccain. Votes) Console. Write. Line("Overall: Obama"); else Console. Write. Line("Overall: Mc. Cain"); } Copyright 2008 by Pearson Education 23
Token-Based File Parsing Consider a file numbers. txt that contains this text: (Note that ˽ means a blank space, t means a tab) 308. 2˽˽˽˽ 7. 4 ˽˽abc˽ 3. 9˽˽˽ ˽-15. 4˽˽˽ We can think of the file's contents like so: 308. 2˽˽˽˽ 7. 4n˽˽abc˽ 3. 9˽˽˽nt˽-15. 4˽˽˽nn Get ALL the lines (using the Text. Reader. Read. To. End()), use the String. Split method to break the string into an array of individual tokens, then parse (convert) the tokens one by one Copyright 2008 by Pearson Education 24
File input question 5 Test the program to make sure that it handles files that contain non-numeric tokens (by skipping them). For example, it should produce the same output as before when given this input file, numbers 4. txt: 308. 2 hello oops 7. 4 bad 3. 9 stuff -15. 4 4. 7 5. 4 : -) 2. 8 @#*($& Copyright 2008 by Pearson Education 25
Token-Based Parsing Answer public void Slide_26() { char[] delimiters = { ' ', 't', 'n', 'r' }; using (Text. Reader file = new Stream. Reader("Files/numbers 4. txt")) { double sum = 0. 0; string s. File = file. Read. To. End(); string[] tokens. From. File = s. File. Split(delimiters, String. Split. Options. Remove. Empty. Entries); foreach (string token in tokens. From. File) { double d. Num; if (Double. Try. Parse(token, out d. Num)) { sum += d. Num; Console. Write. Line("number = {0}", d. Num); } } Console. Write. Line("Sum = {0}", sum); } } Copyright 2008 by Pearson Education 26
Visual Studio Details You will need to set the current working directory for your programs In Solution Explorer: Right-click on project, then Properties Left-hand column: click on 'Debug' Working Directory is about ½ way down – use the. . . button to browse for 03_PCE_Student. Code Notice that it puts an absolute path into the directory, and not a relative directory All the files you need for this week's PCEs are stored in the 'Files' sub folder of the starter project Copyright 2008 by Pearson Education 27
- Slides: 27