Presentation is loading. Please wait.

Presentation is loading. Please wait.

File IO (Input Output) Slides 41 to 50 are new! Chapter 9 of the book Read sections 9.1 and 9.2.

Similar presentations


Presentation on theme: "File IO (Input Output) Slides 41 to 50 are new! Chapter 9 of the book Read sections 9.1 and 9.2."— Presentation transcript:

1 File IO (Input Output) Slides 41 to 50 are new! Chapter 9 of the book Read sections 9.1 and 9.2

2 Dim strLastNames() As String = {"GUNOPULOS", "PApadopoulos", "Keogh", "vlachos"} Dim strFirstNames() As String = {"dima", "JoHn", "Keogh", "MARY"} Dim blnIsMale() As Boolean = {True, True, True, False} Dim sngGPA() As Single = {1.2, 2.4, 4.0, 3.4} Up to this point, we have either hard coded the information our program used (as below), or lost all information once the program closed… Most real programs don’t do this. Typically programs read in information from files, process the information, and write information back to files. Consider a word processor: We might run the Notepad program We then might open a file, say a homework We might process the file, say edit it, or run a spell check When we are done, we would save the file

3 With Visual Basic, we can open all kinds of files, including sound files, movie files, webpages, spreedsheets etc. However, we will only consider simple, structured text files By structured I mean that the file has well defined conventions. For example: One person per row Exactly 4 fields Order is, last name, first name, sex, GPA Missing values are denoted as “@@” The last name is exactly 25 characters long Dim strLastNames() As String = {"GUNOPULOS", "PApadopoulos", "Keogh", "vlachos"} Dim strFirstNames() As String = {"dima", "JoHn", "Keogh", "MARY"} Dim blnIsMale() As Boolean = {True, True, True, False} Dim sngGPA() As Single = {1.2, 2.4, 4.0, 3.4}

4 A Padded File Note that these are spaces, NOT tabs Note that file ends here

5 C:\Documents and Settings\eamonn.keogh\Desktop\StudentData.txt My file has a path and name. Note that the name includes the extension, in this case “.txt”

6 Imports System.IO

7 Dim myStream As New FileStream("C:\Documents and Settings\eamonn.keogh\Desktop\StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) Dim strTempString As String strTempString = myStreamReader.ReadLine() bntRedDemo.Text = strTempString Dim myStream As New FileStream("C:\Documents and Settings\eamonn.keogh\Desktop\StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) Dim strTempString As String strTempString = myStreamReader.ReadLine() bntRedDemo.Text = strTempString

8 Dim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) Dim strTempString As String strTempString = myStreamReader.ReadLine() bntRedDemo.Text = strTempString Dim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) Dim strTempString As String strTempString = myStreamReader.ReadLine() bntRedDemo.Text = strTempString A little “trick”, if you don’t give the path, VB assumes that the file is in the same directory as the program itself.

9 Dim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) This line of code opens the file, read for some action. Why must we explicitly open (and close) files? The path and name of the file to open Append Opens the file if it exists and seeks to the end of the file, or creates a new file. Only works in conjunction with FileAccess.Write. Create Specifies that the operating system should create a new file. Overwrites the file if it exists. CreateNew Specifies that the operating system should create a new file. If the file exists, an error occurs. Open Specifies that the operating system should open an existing file. OpenOrCreate Specifies that the operating system should open a file if it exists; otherwise, a new file should be created. Truncate Specifies that the operating system should open an existing file and clear its contents. The file mode Read Data can be read from the file. Combine with Write for read/write access. ReadWrite Data can be written to and read from the file. Write Data can be written to the file. The file action

10 Dim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) Dim strTempString As String strTempString = myStreamReader.ReadLine() bntRedDemo.Text = strTempString Dim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) Dim strTempString As String strTempString = myStreamReader.ReadLine() bntRedDemo.Text = strTempString If I wanted to write data to the stream, I would use… Dim myStreamReader As New StreamWriter(myStream)

11 Dim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) While myStreamReader.Peek() <> -1 bntRedDemo.Text = myStreamReader.ReadLine() bntRedDemo.Refresh() Sleep(1000) End While Dim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) While myStreamReader.Peek() <> -1 bntRedDemo.Text = myStreamReader.ReadLine() bntRedDemo.Refresh() Sleep(1000) End While …..Peek equals –1 when the end of the file is reached

12 Dim myStream As New FileStream("StudentXX.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) While myStreamReader.Peek() <> -1 bntRedDemo.Text = myStreamReader.ReadLine() bntRedDemo.Refresh() Sleep(1000) End While Dim myStream As New FileStream("StudentXX.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) While myStreamReader.Peek() <> -1 bntRedDemo.Text = myStreamReader.ReadLine() bntRedDemo.Refresh() Sleep(1000) End While File StudentXX.txt does not exist! Trying to open a file that does not exist causes an error.. Note I am using “FileMode.Open”, if I were using one of the other options…

13 If Not (File.Exists("StudentXX.txt")) Then bntRedDemo.Text = "The file does not exist!" Else Dim myStream As New FileStream("StudentXX.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) While myStreamReader.Peek() <> -1 bntRedDemo.Text = myStreamReader.ReadLine() bntRedDemo.Refresh() Sleep(1000) End While End If If Not (File.Exists("StudentXX.txt")) Then bntRedDemo.Text = "The file does not exist!" Else Dim myStream As New FileStream("StudentXX.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) While myStreamReader.Peek() <> -1 bntRedDemo.Text = myStreamReader.ReadLine() bntRedDemo.Refresh() Sleep(1000) End While End If I won’t show this test for existence in future slides (to save space ), but it is always our responsibility to test for a file’s existence before opening it.

14 Dim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) While myStreamReader.Peek() <> -1 bntRedDemo.Text = myStreamReader.ReadLine() bntRedDemo.Refresh() Sleep(1000) End While myStream.Close() myStreamReader.Close() Dim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) While myStreamReader.Peek() <> -1 bntRedDemo.Text = myStreamReader.ReadLine() bntRedDemo.Refresh() Sleep(1000) End While myStream.Close() myStreamReader.Close() We need to close files/streams as soon as we are done with them

15 So far we have been reading files a whole line at a time, as a single string. We need to be able to “get at” individual parts

16 Last names begin at 0 First names begin at 24 Sex begins at 40 GPA begins at 48 The last line is for demonstration only. Don’t add such a line to your code

17 Dim strTempString, strLName, strFName, strSex As String Dim sngGPA As Single strTempString = myStreamReader.ReadLine() strLName = Trim(strTempString.Substring(0, 24)) strFName = Trim(strTempString.Substring(24, 16)) strSex = Trim(strTempString.Substring(40, 8)) sngGPA = Val(strTempString.Substring(48, 3)) If UCase(strSex) = "M" Then bntRedDemo.Text = "Mr. " & strFName & " " & strLName & " has a GPA of " & Str(sngGPA) Else bntRedDemo.Text = "Ms. " & strFName & " " & strLName & " has a GPA of " & Str(sngGPA) End If Dim strTempString, strLName, strFName, strSex As String Dim sngGPA As Single strTempString = myStreamReader.ReadLine() strLName = Trim(strTempString.Substring(0, 24)) strFName = Trim(strTempString.Substring(24, 16)) strSex = Trim(strTempString.Substring(40, 8)) sngGPA = Val(strTempString.Substring(48, 3)) If UCase(strSex) = "M" Then bntRedDemo.Text = "Mr. " & strFName & " " & strLName & " has a GPA of " & Str(sngGPA) Else bntRedDemo.Text = "Ms. " & strFName & " " & strLName & " has a GPA of " & Str(sngGPA) End If Raw data One problem with the above is “magic numbers”…

18 Up to now we have been working with padded files (also called fixed width files), let us now consider comma delimited files.

19 Important note! My way of dealing with comma delimited files is different to way given in the book. Ignore the book on comma delimited files (Section 9.3)

20 There is nothing magic about commas. We could us other character, so long as it does not appear in any of the fields. The separator is sometimes called an escape character. Padded Files vs Comma Delimited files. Padded files are easier for humans to read. Padded files require that we have a maximum length for each field. Comma Delimited files take up less space. Comma Delimited files do not allow random access (Explanation later)

21 Dim myStream As New FileStream("StudentData2.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) Dim strTempString, strLName, strFName, strSex As String Dim sngGPA As Single strTempString = myStreamReader.ReadLine() bntRedDemo.Text = strTempString myStream.Close() myStreamReader.Close() Dim myStream As New FileStream("StudentData2.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream) Dim strTempString, strLName, strFName, strSex As String Dim sngGPA As Single strTempString = myStreamReader.ReadLine() bntRedDemo.Text = strTempString myStream.Close() myStreamReader.Close() We can read comma delimited files, just like we read padded files… But we cannot extract the fields in the same way.

22 There is a function called InStr, which looks for a substring in a longer string, and returns its location Syntax: Integer = InStr(String,String) Function call Return Value InStr(“This is a very”, “is”) 3 InStr(“ab ab ab”, “ab”) 1 InStr(“ab ab ab”, “a”) 1 InStr(“ab ab ab”, “c”) 0 Function Name: InStr Function Description: Returns the position of the first occurrence of a substring that is searched for in the String passed. Common Uses: InStr can be used to tell us if a String has a certain substring contained within it. It operates much like searching a document for a word. Syntax: Long = InStr(String to be Searched, Search String) Examples: Review, we have seen this slide before…

23 There is a function called Mid, which returns a subsection of a string… Function call Return Value Mid(“This is the String”, 6, 2) “is” Mid(“This is the String”, 9, 3) “the” Dim shtP As Short = 4 Mid(“This is the String”, 13, shtP ) “Stri” Mid(“This is the String”, 8) “ the String” Syntax: String = Mid(String, integer_type, integer_type ) Optional! Returns a specific number of characters of a String allowing the developer to indicate where to start and how many characters to return. The first parameter is the source String. The second is an Integer indicating the starting position to copy from. The third parameter is optional and indicates the number of characters to copy. If the third parameter is left out, all characters from the starting position are returned. Review, we have seen this slide before…

24 strTempString = myStreamReader.ReadLine() bntRedDemo.Text = InStr(strTempString, ",") strTempString = myStreamReader.ReadLine() bntRedDemo.Text = InStr(strTempString, ",") This is a little cryptic.. (see next slide)

25 Const mySEPARATOR = "," strTempString = myStreamReader.ReadLine() bntRedDemo.Text = InStr(strTempString, mySEPARATOR) Const mySEPARATOR = "," strTempString = myStreamReader.ReadLine() bntRedDemo.Text = InStr(strTempString, mySEPARATOR)

26 Dim shtFrom, shtLen As Short strTempString = myStreamReader.ReadLine() shtFrom = 1 shtLen = InStr(strTempString, mySEPARATOR) strLName = Mid(strTempString, shtFrom, shtlen) bntRedDemo.Text = strLName Dim shtFrom, shtLen As Short strTempString = myStreamReader.ReadLine() shtFrom = 1 shtLen = InStr(strTempString, mySEPARATOR) strLName = Mid(strTempString, shtFrom, shtlen) bntRedDemo.Text = strLName Lets work on just getting the first field… This is almost right, but we have one extra character…

27 Dim shtFrom, shtLen As Short strTempString = myStreamReader.ReadLine() shtFrom = 1 shtLen = InStr(strTempString, mySEPARATOR) - 1 strLName = Mid(strTempString, shtFrom, shtLen) bntRedDemo.Text = strLName Dim shtFrom, shtLen As Short strTempString = myStreamReader.ReadLine() shtFrom = 1 shtLen = InStr(strTempString, mySEPARATOR) - 1 strLName = Mid(strTempString, shtFrom, shtLen) bntRedDemo.Text = strLName This works…

28 GUNOPULOS,dima,M,1.2 We can pull out the first field because we know: Where it begins, at 1 Where it ends, at the first comma (-1) shtFrom = 1 shtLen = InStr(strTempString, mySEPARATOR) - 1 strLName = Mid(strTempString, shtFrom, shtLen) shtFrom = 1 shtLen = InStr(strTempString, mySEPARATOR) - 1 strLName = Mid(strTempString, shtFrom, shtLen) What about the second field? We know the beginning, it is just the end of the previous field, plus two. However, we don’t have an easy way to get the end…

29 Dim shtFrom, shtLen As Short strTempString = myStreamReader.ReadLine() shtFrom = 1 shtLen = InStr(strTempString, mySEPARATOR) - 1 strLName = Mid(strTempString, shtFrom, shtLen) strTempString = Mid(strTempString, shtlen + 2) shtlen = InStr(strTempString, mySEPARATOR) - 1 strFName = Mid(strTempString, shtFrom, shtlen) bntRedDemo.Text = "Mr. " & strFName & " " & strLName Dim shtFrom, shtLen As Short strTempString = myStreamReader.ReadLine() shtFrom = 1 shtLen = InStr(strTempString, mySEPARATOR) - 1 strLName = Mid(strTempString, shtFrom, shtLen) strTempString = Mid(strTempString, shtlen + 2) shtlen = InStr(strTempString, mySEPARATOR) - 1 strFName = Mid(strTempString, shtFrom, shtlen) bntRedDemo.Text = "Mr. " & strFName & " " & strLName GUNOPULOS,dima,M,1.2 dima,M,1.2

30 GUNOPULOS,dima,M,1.2 dima,M,1.2 M,1.2 1.2 This is our basic trick for reading comma delimited files. We read an entire line. 1. We read the first field (up to the first comma). 2. We remove the first field (plus the first comma) 3.If we are not done, we go back to step one

31 strTempString = myStreamReader.ReadLine() shtFrom = 1 shtLen = InStr(strTempString, mySEPARATOR) - 1 strLName = Mid(strTempString, shtFrom, shtLen) strTempString = Mid(strTempString, shtlen + 2) shtlen = InStr(strTempString, mySEPARATOR) - 1 strFName = Mid(strTempString, shtFrom, shtlen) strTempString = Mid(strTempString, shtlen + 2) shtlen = InStr(strTempString, mySEPARATOR) - 1 strSex = Mid(strTempString, shtFrom, shtlen) strTempString = myStreamReader.ReadLine() shtFrom = 1 shtLen = InStr(strTempString, mySEPARATOR) - 1 strLName = Mid(strTempString, shtFrom, shtLen) strTempString = Mid(strTempString, shtlen + 2) shtlen = InStr(strTempString, mySEPARATOR) - 1 strFName = Mid(strTempString, shtFrom, shtlen) strTempString = Mid(strTempString, shtlen + 2) shtlen = InStr(strTempString, mySEPARATOR) - 1 strSex = Mid(strTempString, shtFrom, shtlen) GUNOPULOS,dima,M,1.2 dima,M,1.2 M,1.2 Note the repetition in the code, we can simply cut and paste the block of 3 lines of code, changing on the variable name of the field (we could push these 3 lines into a function…)

32 strTempString = Mid(strTempString, shtlen + 2) shtlen = InStr(strTempString, mySEPARATOR) - 1 strFName = Mid(strTempString, shtFrom, shtlen) strTempString = Mid(strTempString, shtlen + 2) shtlen = InStr(strTempString, mySEPARATOR) - 1 strSex = Mid(strTempString, shtFrom, shtlen) strTempString = Mid(strTempString, shtlen + 2) sngGPA = Mid(strTempString, shtFrom) strTempString = Mid(strTempString, shtlen + 2) shtlen = InStr(strTempString, mySEPARATOR) - 1 strFName = Mid(strTempString, shtFrom, shtlen) strTempString = Mid(strTempString, shtlen + 2) shtlen = InStr(strTempString, mySEPARATOR) - 1 strSex = Mid(strTempString, shtFrom, shtlen) strTempString = Mid(strTempString, shtlen + 2) sngGPA = Mid(strTempString, shtFrom) dima,M,1.2 The last field is special, because the end is not marked with a comma, we just take the entire string We could write this as one line: sngGPA = Mid(strTempString, shtlen + 2) 1.2 M,1.2

33 ….

34 So far we have read files, printed them out one line at a time and immediately forgotten the information! More realistically, we would want to keep all the data around to process it (sort it, update the GPAs, look for a particular person, look for the lowest GPA…) How to we keep “lists” of data? With arrays. In the past we hardcoded arrays (as below), but now we can populate arrays by reading files. Dim strLastNames() As String = {"GUNOPULOS", "PApadopoulos", "Keogh", "vlachos"} Dim strFirstNames() As String = {"dima", "JoHn", "Keogh", "MARY"} Dim blnIsMale() As Boolean = {True, True, True, False} Dim sngGPA() As Single = {1.2, 2.4, 4.0, 3.4}

35 Note that little has changed from the previous code…

36 Note the order, you cannot reverse it

37

38 Wrong sex

39

40 Last names begin at 0 First names begin at 24 Sex begins at 40 GPA begins at 48 Padded files allow random access. Comma delimited files require sequential access

41 Writing padded files…

42 Last names begin at 0 First names begin at 24 Sex begins at 40 GPA begins at 48 The last line is for demonstration only. Don’t add such a line to your code Review, we have seen this slide before…

43 Dim strTempString As String strTempString = “Eamonn” strTempString = strTempString.PadRight Dim strTempString As String strTempString = “Eamonn” strTempString = strTempString.PadRight The variable strTempString now has the value “Eamonn ” Note the four extra spaces. There is also a PadLeft function

44 Dim strTempString, strLName, strFName, strSex As String Dim sngGPA As Single strTempString = myStreamReader.ReadLine() strLName = Trim(strTempString.Substring(0, 24)) strFName = Trim(strTempString.Substring(24, 16)) strSex = Trim(strTempString.Substring(40, 8)) sngGPA = Val(strTempString.Substring(48, 3)) If UCase(strSex) = "M" Then bntRedDemo.Text = "Mr. " & strFName & " " & strLName & " has a GPA of " & Str(sngGPA) Else bntRedDemo.Text = "Ms. " & strFName & " " & strLName & " has a GPA of " & Str(sngGPA) End If Dim strTempString, strLName, strFName, strSex As String Dim sngGPA As Single strTempString = myStreamReader.ReadLine() strLName = Trim(strTempString.Substring(0, 24)) strFName = Trim(strTempString.Substring(24, 16)) strSex = Trim(strTempString.Substring(40, 8)) sngGPA = Val(strTempString.Substring(48, 3)) If UCase(strSex) = "M" Then bntRedDemo.Text = "Mr. " & strFName & " " & strLName & " has a GPA of " & Str(sngGPA) Else bntRedDemo.Text = "Ms. " & strFName & " " & strLName & " has a GPA of " & Str(sngGPA) End If Raw data One problem with the above is “magic numbers”… Review, we have seen this slide before…

45 Dim myOutStream As New FileStream("StudentDataNew2.txt", FileMode.Create, FileAccess.Write) Dim myStreamWriter As New StreamWriter(myOutStream) For shtIndex = 0 To UBound(strLastNames) If blnIsMale(shtIndex) Then strTempString = strLastNames(shtIndex).PadRight(23) strTempString &= strFirstNames(shtIndex).PadRight(16) strTempString &= ("M").PadRight(8) strTempString &= sngGPA(shtIndex).ToString() Else strTempString = strLastNames(shtIndex).PadRight(23) strTempString &= strFirstNames(shtIndex).PadRight(16) strTempString &= ("F").PadRight(8) strTempString &= sngGPA(shtIndex).ToString() End If myStreamWriter.WriteLine(strTempString) Next myStreamWriter.Close() myOutStream.Close() Dim myOutStream As New FileStream("StudentDataNew2.txt", FileMode.Create, FileAccess.Write) Dim myStreamWriter As New StreamWriter(myOutStream) For shtIndex = 0 To UBound(strLastNames) If blnIsMale(shtIndex) Then strTempString = strLastNames(shtIndex).PadRight(23) strTempString &= strFirstNames(shtIndex).PadRight(16) strTempString &= ("M").PadRight(8) strTempString &= sngGPA(shtIndex).ToString() Else strTempString = strLastNames(shtIndex).PadRight(23) strTempString &= strFirstNames(shtIndex).PadRight(16) strTempString &= ("F").PadRight(8) strTempString &= sngGPA(shtIndex).ToString() End If myStreamWriter.WriteLine(strTempString) Next myStreamWriter.Close() myOutStream.Close()

46 Dim myOutStream As New FileStream("StudentDataNew2.txt", FileMode.Create, FileAccess.Write) Dim myStreamWriter As New StreamWriter(myOutStream) For shtIndex = 0 To UBound(strLastNames) strTempString = strLastNames(shtIndex).PadRight(23) strTempString &= strFirstNames(shtIndex).PadRight(16) If blnIsMale(shtIndex) Then strTempString &= ("M").PadRight(8) Else strTempString &= ("F").PadRight(8) End If strTempString &= sngGPA(shtIndex).ToString() myStreamWriter.WriteLine(strTempString) Next myStreamWriter.Close() myOutStream.Close() Dim myOutStream As New FileStream("StudentDataNew2.txt", FileMode.Create, FileAccess.Write) Dim myStreamWriter As New StreamWriter(myOutStream) For shtIndex = 0 To UBound(strLastNames) strTempString = strLastNames(shtIndex).PadRight(23) strTempString &= strFirstNames(shtIndex).PadRight(16) If blnIsMale(shtIndex) Then strTempString &= ("M").PadRight(8) Else strTempString &= ("F").PadRight(8) End If strTempString &= sngGPA(shtIndex).ToString() myStreamWriter.WriteLine(strTempString) Next myStreamWriter.Close() myOutStream.Close() The previous version had redundant code, this is better. (why is redundant code bad?)

47 Two possible problems. 1)A space before “Eamonn”, we can use trim 2)“4.0” was formatted and “4” The second point could be a major problem. If other programs are going to look at our file, and they don’t automatically convert spaces to zeros…

48 We can fix the problem with a format specifier strTempString &= sngGPA(shtIndex).ToString("N1")

49 SpecifierNameDescription Code CCurrencyFormats with a dollar sign, commas, two decimal places Negative values are in () FFixed-PointFormats as a string of numeric digits, no commas, two decimal places, a minus sign at the left for negative values NNumberFormats with commas, two decimal places, a minus sign at the left for negative values DDigitsUse only for integer data types. Formats with a left minus sign for negative values. PPercentMultiplies the value by 100, add a space and percent sign, and rounds to two decimal places Visual Basics Format Specifiers

50 VariableValueFormatOutput totalDecimal1125.6744C$1,125.67 totalDecimal1125.6744N1,125.67 totalDecimal1125.6744N01,126 balanceDecimal1125.6744N31,125.674 balanceDecimal1125.6744F01,126 pinInteger123D6000123 rateDecimal0.075P7.50% rateDecimal0.075P37.500% rateDecimal0.075P08% valueInteger-10C($10.00) valueInteger-10N-10.00 valueInteger-10D3-010 Examples


Download ppt "File IO (Input Output) Slides 41 to 50 are new! Chapter 9 of the book Read sections 9.1 and 9.2."

Similar presentations


Ads by Google