Presentation is loading. Please wait.

Presentation is loading. Please wait.

Text / Serial / Sequential Files

Similar presentations


Presentation on theme: "Text / Serial / Sequential Files"— Presentation transcript:

1 Text / Serial / Sequential Files
17/01/2019

2 Learning Objectives Explain what a file does and why a file is useful.
State the restrictions of a text file. Give the general form of code used to work with files. Use the DateDiff function. 17/01/2019

3 A File Stores data permanently:
In all programs so far, any stored data has been lost when the program closes, and has to be entered again when the program next runs. When you have a lot of data to store like this is obviously not feasible. 17/01/2019

4 Text Files Store data as characters represented by their ASCII codes.
American Standard Code for Information Interchange Upper case letters (‘A’ - ‘Z’) Lower case letters (‘a’ – ‘z’) Numeric digits (‘0’ – ‘9’) The space character is 32. 17/01/2019

5 Text / Serial / Sequential Files
Have no natural built in structure to store records since data is simply stored as a sequence of characters. You can open a file to be read from or written to but not both at the same time. Data is read from or written to sequentially: Text files are Sequential files. 17/01/2019

6 Text / Serial / Sequential Access Restrictions
When you read data you have to read all the data from the beginning to the end. When you add data you can only add on the end i.e. append or clear all data and start again. Not really possible to change the data in a text file. 17/01/2019

7 Opening a Text / Serial / Sequential file
You have to open a file before you can use it. FileOpen(1, FileName, OpenMode.Append) FileOpen(1, FileName, OpenMode.Input) FileOpen(1, FileName, OpenMode.Output) Reference number of the file used by VB to identify it. You only need to use a higher number if you wish to open more than one file at a time. Append Write to the end of the file. Input Read from the file. Output Write to the file (clears all previous data). 17/01/2019

8 Filename The full path and name of the file.
It is best to use a variable to hold all this: Dim FileName As String = CurDir() & "\....txt" Name of file.txt Finds the path of the current program, this will store the file in the same folder. It is best to do this otherwise every time you move the program you would have to change the path accordingly. 17/01/2019

9 Writing to a Text / Serial / Sequential file
WriteLine(1, …) Puts quotation marks (“…”) around each string but not numbers, so treats numbers as separate items. Data to be written (on one line). Can add more data by , …, …, … Ref no. PrintLine(1, …) Puts spaces between data and the whole record is treated as one item. 17/01/2019

10 Reading from a Text / Serial / Sequential file
VB uses an imaginary pointer when it reads through a file. When a text file is opened it points to the 1st line. 17/01/2019

11 Reading from a Text / Serial / Sequential file
Input(1, …) Reads single data items, item by item. Imaginary pointer moves from item to item. … = LineInput(1) Reads lines of data, line by line. Imaginary pointer moves from line to line Ref no. Variable to which data is assigned. 17/01/2019

12 Reading from a Text / Serial / Sequential file
Input or LineInput should be contained within a loop (to ensure that you don’t go read past the end of a file and force an error). Do While Not EOF(1) Input(1, …) Or … = LineInput(1) Rest of loop body statements. Loop EOF = End Of File This will loop through the whole file.

13 Closing a file FileClose(1) Ref no. 17/01/2019

14 Program 7.1a Text / Serial / Sequential File
Specification: Allow the user to enter people’s names and dates of birth and store these in a text file. Allow the user to display the contents of this file at any time and count the number of adults on file (18 or over). 17/01/2019

15 Program 7.1a Text / Serial / Sequential File
‘Declare a variable to hold the name and path of the file. Dim FileName As String = CurDir() & "\NamesAndAges.txt“ Dim Name As String = “” Dim DOB As Date Dim EnterAnotherStudent As Boolean Dim Request As String Dim Again As Boolean Dim Age As Integer Dim NumberOfAdults As Integer 17/01/2019

16 Program 7.1a Text / Serial / Sequential File
Do Console.WriteLine(“Do you wish to ‘Add’, ‘Display’ or ‘Reset’?”) Request = Console.ReadLine If Request = “Add” Then Console.WriteLine(“Please enter the person’s name.”) Name = Console.ReadLine Console.WriteLine(“Please the person’s DOB.”) DOB = Console.ReadLine ‘Open the file to add new data. FileOpen(1, FileName, OpenMode.Append) WriteLine(1, Name, DOB) ‘Add the name and age to the file. FileClose(1) ‘Close the file. Console.WriteLine(“Do you wish to enter another person (True/False)?”) EnterAnotherStudent = Console.ReadLine Loop Until EnterAnotherStudent = False 17/01/2019

17 Program 7.1a Text / Serial / Sequential File
ElseIf Request = “Display” Then FileOpen(1, FileName, OpenMode.Input) ‘Open the file for reading. Do While Not EOF(1) ‘Loop through the file to the end. Input (1, Name) ‘Read the name. Input (1, DOB) ‘Read the DOB. Age = DateDiff(DateInterval.Year, DOB, Today) ‘Calculate the Age. Console.WriteLine(Name & “ ” & Age) ‘Add name and age to the list box. If Age >= 18 Then ‘Count the number of adults. NumberOfAdults +=1 End If Loop FileClose(1) ‘Close the file. ‘Output the number older than 18 at the end of the list. Console.WriteLine("Number of adults: " & NumberOfAdults) 17/01/2019

18 Program 7.1 Text / Serial / Sequential File
ElseIf Request = “Reset” Then ‘Clear the file. FileOpen(1, FileName, OpenMode.Output) FileClose(1) ‘Close the file. End If Console.WriteLine(“Do you wish to continue (True/False)?”) Again = Console.ReadLine Loop Until Again = False 17/01/2019

19 Program 7.1a Text / Serial / Sequential File
Run the program and test it. Do you know why we have not used Print for writing to or LineInput to read from the file? Try using Print for writing to and LineInput to read from the file to see what happens if you are not sure. Why does the program crash? Please explain the reasons why we should not use Print and LineInput in this situation, in your comments. Just to make sure you change back when you are done. 17/01/2019

20 How to find the file manually.
As explained, if CurDir() is used then files are stored relative to the program; this is always in the Program’s folder: There will be another folder inside the initial Program’s folder e.g. Text File – Text File. bin Debug The file should be visible here and you can simply double click to open it in your default text file editor e.g. Notepad.

21 Commenting on Files In presentations 7.1 – 7.2 I will only ask for comments to files. Your comments MUST explain: What is the file for? And when it is being used: What are you doing, storing or retrieving? When (after and before what) are you doing this and why does it have to be done there? When in the procedure code? 17/01/2019

22 Extension 7.1b: “Reading Habits”
Anna wants to find out about her fellow students’ reading habits. It will be part of her Literature coursework. She will ask questions online, so starts by designing a screen layout. The first four questions will ask for: student’s first name date of birth type of book they prefer (printed, audio-book or e-book) whether student reads novels (yes/no) The responses from each student will be stored as a record consisting of the following fields: FirstName DateOfBirth BookType ReadsNovels Anna is to write a program to analyse the responses. Write a program to input the responses and then to calculate the totals for each BookType (printed, audio-book or e-book). 17/01/2019

23 Extension 7.1c: “AS Computing Students”
Write a program to store all the names (first and last) of students studying AS Computing in a text file. A user should be able to add more students to the list if they wish to. Use either PrintLine or WriteLine. A user should be able to click a button to see the list in a list box. 17/01/2019

24 Plenary Explain what a file does and why a file is useful.
Store data permanently. So data does need to be re-entered every time the program starts. 17/01/2019

25 Plenary What are the restrictions of a text file? 17/01/2019

26 Text Files Have no natural built in structure to store records since data is simply stored as a sequence of characters. You can open a file to be read from or written to but not both at the same time. Data is read from or written to sequentially: Text files are Sequential files. 17/01/2019

27 Sequential Access Restrictions
When you read data you have to read all the data from the beginning to the end. When you add data you can only add on the end i.e. append or clear all data and start again. Not really possible to change the data in a text file. 17/01/2019

28 Plenary What is the general form of code used to work with files?
Dim FileName As String = My.Application.Info.DirectoryPath _ & "\....txt" FileOpen(1, FileName, OpenMode.Input/Append/Output) Do While Not EOF(1) WriteLine(1, …) / PrintLine(1,…) … = LineInput(1) / Input(1, …) Rest of Loop Body Loop FileClose(1) 17/01/2019


Download ppt "Text / Serial / Sequential Files"

Similar presentations


Ads by Google